Jump to content
Sign in to follow this  
cburak

Copying an Array into an Array

Recommended Posts

cburak

Hey Iam trying to copy an Array in to another Array with no luck.

I have an ArrayA(2dimension) and ArrayB(1Dimension) I want to copy whole ArrayB in ArrayA.

Global $GradeArray[8][2]
$GradeArray[0] = StringSplit($Text,"   ",3)

Stringsplit returns 1dimension array with 2 elements and GradeArray have 2dimension. But this code only gives me error.

Do I have to copy 1 element by 1 like this?:

$i = StringSplit($Text,"   ",3)
    $GradeArray[0][0] =$i[0]
    $GradeArray[0][1] =$i[1]

Or simply Iam just making simple syntax mistake?

Thanks for any advice.

Edited by cburak

Share this post


Link to post
Share on other sites
MHz

Multi Dimensional arrays are fun. :P Well, sometimes

See if this helps

; declare A and B
Global $GradeArrayA[8][2], $GradeArrayB[8]

; assign items to B
For $1 = 0 To UBound($GradeArrayB) -1
    $GradeArrayB[$1] = "abc   def"
Next

; check that size is aligned in the arrays
If UBound($GradeArrayA) = UBound($GradeArrayB) And UBound($GradeArrayA, 2) = 2 Then
    For $1 = 0 To UBound($GradeArrayA, 1) -1
        ; split the element in B
        $2 = StringSplit($GradeArrayB[$1], "   ", 3)
        ; assign the split into A
        $GradeArrayA[$1][0] = $2[0]
        $GradeArrayA[$1][1] = $2[1]
        ; debug output
        ConsoleWrite($GradeArrayA[$1][0] & @CRLF)
        ConsoleWrite($GradeArrayA[$1][1] & @CRLF)
    Next
EndIf

:)

Share this post


Link to post
Share on other sites
cburak

Thanks for your effort, your sample perfectly works. I tought I have syntax mistake but now I realise I "have" to copy 1 by 1.

Share this post


Link to post
Share on other sites
kylomas

cburak,

Some other alternatives...

#include <array.au3>


;----------------------------------------------------------------------------
; 1ST method - constuct a 2D array of student and grades
;   This is similar to Mhz's example except that grades by student are
;      formatted across columns, not rows.
;   Number of 2D array columns determined by input
;----------------------------------------------------------------------------

; Input
local $text = 'George,A,C,F,B|Alex,D,B,A,A,F|Susan,A,,B,A|Gomer,D,F,DROPPED'

local $iCol = 0
$aTemp1 = stringsplit($text,'|',2)

; determine max # of cols
for $1 = 0 to ubound($aTemp1) - 1
    $aTemp2 = stringsplit($aTemp1[$1],',')
    if $iCol < $aTemp2[0] then $iCol = $aTemp2[0]
Next

; define 2D array based on # of rows split and max # of cols
local $Students[ubound($aTemp1)][$iCol]
ConsoleWrite('Student array defined with ' & ubound($aTemp1) & ' rows and ' & $iCol & ' columns.' & @LF)

; load the resulting array ($Students)
for $1 = 0 to ubound($aTemp1) - 1
    $aTemp2 = stringsplit($aTemp1[$1],',',2)
    for $2 = 0 to ubound($aTemp2) - 1
        $Students[$1][$2] = $aTemp2[$2]
    Next
Next

_arraydisplay($Students,'Student Grades')

;----------------------------------------------------------------------------------------------
; 2ND method - You can assign an array to an element of another array (as you originally tried)
;----------------------------------------------------------------------------------------------

local $aCars[4][2] = [ _
                        ['Toyota',''], _
                        ['Ford',''], _
                        ['Mazeratti',''], _
                        ['Yugo',''] _
                        ]

local $aOptionsMazerrati[4] = ['Red','Chrome Wheels','Delux Stereo','Leather']

for $1 = 0 to ubound($aCars) - 1
    if $aCars[$1][0] = 'Mazeratti' then $aCars[$1][1] = $aOptionsMazerrati
Next

_arraydisplay($aCars,'Cars')
_arraydisplay($aCars[2][1],'Options for Mazerrati')

; write array contents to console
ConsoleWrite(@lf & '! Cars' & @LF)
for $1 = 0 to ubound($aCars) - 1
    ConsoleWrite($aCars[$1][0] & @LF)
    if isarray($aCars[$1][1]) then
        $aTmp = $aCars[$1][1]   ; assign array to temp array because syntax does not allow it to be addressed directly
        for $2 = 0 to ubound($aTmp) - 1
            consolewrite(@tab & $aTmp[$2] & @lf)
        Next
    endif
Next

kylomas

edit: added / corrected comments

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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
Sign in to follow this  

×