Jump to content
Acce

Adding 6 Vars into an array

Recommended Posts

Hi guys I started on a huge project and it became more complex then I thought , I was hoping to avoid ussing arrays but looks like I need it now 

 

I Have ended up with 6 Vars Output like so 

$g1 = W|2|3|W|X
$g2 = 1|X|W|4|X
$g3 = 1|2|W|4|X
$g4 = 
$g5 = W|2|3|4|X
$g6 = 

 

(notice some blanks, its random which of these vars that empty and will be different from each time)

To work better with my Data I think I need to get all of this data into an array , Tried something like this :

 

  Local  $arr[6][6] = [[StringSplit($g1, '|')], [StringSplit($g2, '|')], [StringSplit($g3, '|')], [StringSplit($g4, '|')], [StringSplit($g5, '|')], [StringSplit($g6, '|')]]
      For $i = 0 to UBound( $arr, 1) - 1
     For $j = 0 to UBound($arr, 2) - 1
       debug($arr[$i][$j])
     Next
  Next

 

But this gave me nothing in my log file :(

 

Share this post


Link to post
Share on other sites

An Alternative to my problem, would be to loop the vars 1-6 if that is possible ? 

Share this post


Link to post
Share on other sites

To get fast and reliable help, you should always provide a fully runable snippet.  With what you have provided us, we can guess (without being sure) what is your issue.  So we need to work to make it runable.  And it is not particularly fun.  But here you go :

#include <Constants.au3>

$g1 = "W|2|3|W|X"
$g2 = "1|X|W|4|X"
$g3 = "1|2|W|4|X"
$g4 = ""
$g5 = "W|2|3|4|X"
$g6 = ""
$g7 = "W|2"
$g8 = "1|2|3"

Local $arr [8][5], $tmp

For $i = 0 to UBound($arr, 1)-1
  $tmp = StringSplit (eval("g" & $i+1),"|", $STR_NOCOUNT)
  For $j = 0 to UBound($tmp)-1
    $arr[$i][$j] = $tmp[$j]
    ConsoleWrite ($arr[$i][$j]  & " ")
  Next
  ConsoleWrite (@CRLF)
Next

 

Share this post


Link to post
Share on other sites
5 minutes ago, Nine said:

To get fast and reliable help, you should always provide a fully runable snippet.  With what you have provided us, we can guess (without being sure) what is your issue.  So we need to work to make it runable.  And it is not particularly fun.  But here you go :

#include <Constants.au3>

$g1 = "W|2|3|W|X"
$g2 = "1|X|W|4|X"
$g3 = "1|2|W|4|X"
$g4 = ""
$g5 = "W|2|3|4|X"
$g6 = ""
$g7 = "W|2"
$g8 = "1|2|3"

Local $arr [8][5], $tmp

For $i = 0 to UBound($arr, 1)-1
  $tmp = StringSplit (eval("g" & $i+1),"|", $STR_NOCOUNT)
  For $j = 0 to UBound($tmp)-1
    $arr[$i][$j] = $tmp[$j]
    ConsoleWrite ($arr[$i][$j]  & " ")
  Next
  ConsoleWrite (@CRLF)
Next

 

Hi thanks for your reply , My issue is how to set up the array as I dont know what im doing , Not sure why you added var $g7 and g8 as those were not in my sample ? 

 

What I want is var g1-g6 to be inserted into a array so I can easily work with the data from there , Here is my latest test not able to get any data returned 

 Local  $arr[2][5] = [[StringSplit($g1, "|")],[StringSplit($g2, "|")],,[StringSplit($g3, "|")],,[StringSplit($g4, "|")],,[StringSplit($g5, "|")],,[StringSplit($g6, "|")]]
 
    For $i = 0 to UBound( $arr, 1) - 1
     For $j = 0 to UBound( $arr, 2) - 1
       debug("$arr[" & $i & "][" & $j & "]:=" & $arr[$i][$j] & @LF)
     Next
  Next

Share this post


Link to post
Share on other sites

I added those 2 lines to show you how different string sizes can be managed. Just remove the 2 lines and make the array dimensions as you want, like this :

#include <Constants.au3>
#include <Array.au3>

$g1 = "W|2|3|W|X"
$g2 = "1|X|W|4|X"
$g3 = "1|2|W|4|X"
$g4 = ""
$g5 = "W|2|3|4|X"
$g6 = ""

Local $arr [6][5], $tmp

For $i = 0 to UBound($arr, 1)-1
  $tmp = StringSplit (eval("g" & $i+1),"|", $STR_NOCOUNT)
  For $j = 0 to UBound($tmp)-1
    $arr[$i][$j] = $tmp[$j]
    ConsoleWrite ($arr[$i][$j]  & " ")
  Next
  ConsoleWrite (@CRLF)
Next
_ArrayDisplay ($arr)

 

Share this post


Link to post
Share on other sites

Thanks to both of you for help, Really liked this part of the code:

_ArrayDisplay ($arr)

Ended up using MVPs code , which also showed me how to use eval which I couldn't get to work previously either . Much appreciated think my data will be much easier to work with now

Share this post


Link to post
Share on other sites

Can I ask for a follow up question here , without creating a new thread ? my brains gets liquefied when working with this , lol.I want to analyze this array we just created, looping through the rows and count numbers, and add a new row (6) with result 

so if the data is like so 

 

Row col 0 col 1 col 2 col 3 col 4
row 0          
row 1          
row 2 1 x w w 5
row 3 1 x w w 5
row 4 w x 3 4 x
row 5          

 

I would want the updated array to look like this :

Row col 0 col 1 col 2 col 3 col 4 New Col
row 0            
row 1            
row 2 1 x w w 5 2
row 3 1 x w w 5 2
row 4 w x 3 4 x 2
row 5            
New R6 2 0 1 1 2  

 

Share this post


Link to post
Share on other sites

Your new question in this thread probably would have got more attention by creating a new thread.

Here is one example of updating an array - the first method I thought of.  There are surely other methods.

#include <Array.au3>

Local $aData[][] = [ _
        [""], _
        [""], _
        [1, "x", "w", "w", 5], _
        [1, "x", "w", "w", 5], _
        ["w", "x", 3, 4, "x"], _
        [""]]
Local $sAppendNewRow = "2|0|1|1"
Local $sAppendNBRows = "2" ; String to append to Non-Blank rows.

; --- Append new last row.---
_ArrayAdd($aData, $sAppendNewRow)

; --- Append new column ----
ReDim $aData[UBound($aData, 1)][UBound($aData, 2) + 1] ; Re-dimension Array with one extra column.

; ---Append $sAppendNBRows value to the end of each non-blank row. ---
Local $sStrData = _ArrayToString($aData) ; Assign contents of array to string for later RE string manipulation.
;ConsoleWrite($sStrData & @CRLF) ; See workings.

Local $aData[0][UBound($aData, 2)] ; Re-assign the array, $aData, with zero rows.
_ArrayAdd($aData, StringRegExpReplace($sStrData, "(?m)([^\|\v]+\|)(\|{0," & (UBound($aData, 2) - 1) & "})$", "${1}" & $sAppendNBRows & "${2}")) ; Append a "2" to all non-blank rows.

;ConsoleWrite(StringRegExpReplace($sStrData, "(?m)([^\|\v]+\|)(\|{0," & (UBound($aData, 2) - 1) & "})$", "${1}2${2}") & @CRLF) ; See workings.
_ArrayDisplay($aData)

 

Share this post


Link to post
Share on other sites

Here is another method in this example.

#include <Array.au3>

Local $aData[][] = [ _
        [""], _
        [""], _
        [1, "x", "w", "w", 5], _
        [1, "x", "w", "w", 5], _
        ["w", "x", 3, 4, "x"], _
        [""]]
Local $sAppendNewRow = "2|0|1|1"
Local $sAppendNBRows = "2" ; String to append to Non-Blank rows.

ReDim $aData[UBound($aData, 1) + 1][UBound($aData, 2) + 1] ; Re-dimension array with one extra row and one extra column.

; -------- Add last row --------
Local $aLastRow = StringSplit($sAppendNewRow, "|", 2) ;  $STR_NOCOUNT (2)
For $i = 0 To UBound($aLastRow) - 1
    $aData[UBound($aData) - 1][$i] = $aLastRow[$i]
Next

; ---Append $sAppendNBRows value to the end of each non-blank row. ---
For $i = 0 To UBound($aData) - 1
    For $j = UBound($aData, 2) - 2 To 0 Step -1
        If $aData[$i][$j] <> "" Then
            $aData[$i][$j + 1] = $sAppendNBRows ; Add $sAppendNBRows to previous column, "$j + 1" in row, "$i".
            ExitLoop
        EndIf
    Next
Next

_ArrayDisplay($aData)

 

Share this post


Link to post
Share on other sites
#include ".\Eigen4AutoIt.au3"

$r0 = ""
$r1 = ""
$r2 = "1|x|w|w|5"
$r3 = "1|x|w|w|5"
$r4 = "w|x|3|4|X"
$r5 = ""
Local $arr[0][5]
_ArrayAdd($arr, $r0 & @CRLF & $r1 & @CRLF & $r2 & @CRLF & $r3 & @CRLF & $r4 & @CRLF & $r5)


; let's try doing it the matrix way
_Eigen_StartUp("integer")
$matA=_Eigen_CreateMatrix_FromArray($arr)

; replace values with bitmask (0/empty = 0, else 1)
_Eigen_ConditMask_InPlace($matA,"<>",0)

; extend matrix in both directions to store totals
_Eigen_AppendRows_InPlace($matA,1)
_Eigen_AppendCols_InPlace($matA,1)

; store colwise sum in new last row
; colwise = apply to each column in turn
$rowvector=_Eigen_MatrixSpecs_Colwise_Single($matA,2)   ; specID 2 = sum
_Eigen_Copy_Arow_ToBrow($rowvector,$matA,0,_Eigen_GetRows($matA)-1)

; store rowwise sum in new last col
; rowwise = apply to each row in turn
$colvector=_Eigen_MatrixSpecs_Rowwise_Single($matA,2)   ; specID 2 = sum
_Eigen_Copy_Acol_ToBcol($colvector,$matA,0,_Eigen_GetCols($matA)-1)

_Eigen_MatrixDisplay($matA,"result")

_Eigen_CleanUp()

 

Share this post


Link to post
Share on other sites

Hi guys sorry for late response forgot all about that this , was able top solve this myself but intresting to see what you have suggested looks much better then what I did . But thanks a lot for all your help :) 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...