Sign in to follow this  
Followers 0
dromenox

Array problem

8 posts in this topic

qVShDEb.png

What am I doing wrong when adding an array on a bidimencional array without using strings?

Share this post


Link to post
Share on other sites



Your error is 5

 

5 - Number of dimensions for $avArray and $vValue arrays do not match

You're trying to add a 1D array to a 2D array, can't be done.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

To be honest, I don't even know why this is valid...

Local $array[0][6]

Zero rows?

I will add rows dynamically. So what is the correct way to declare a empty bidimensional array?

Share this post


Link to post
Share on other sites

I wouldn't worry about the array declaration, I'd work on the array you're adding.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

a possible alternative:

adds  1 row to a 2D array and fills it with the values of an 1D array

#include <array.au3>
Local $Array[0][6]

Local $tmpArray = [1, 2, 3]
__ArrayAdd_1DRow($array, $tmpArray)
ConsoleWrite(@error & @TAB & @extended & @CRLF)

Local $tmpArray = [1, 2, 3, 4, 5, 6]
__ArrayAdd_1DRow($array, $tmpArray)
ConsoleWrite(@error & @TAB & @extended & @CRLF)

Local $tmpArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]
__ArrayAdd_1DRow($array, $tmpArray)
ConsoleWrite(@error & @TAB & @extended & @CRLF)

_ArrayDisplay($array)

; adds  1 row to a 2D array and fills it with the values of an 1D array
; if 1D array exceeds the number of columns of the 2D array, only columns that fit are added and @extended is set to 1
Func __ArrayAdd_1DRow(ByRef $2D_Array, ByRef $1D_Array)
    If UBound($2D_Array, 0) <> 2 Or UBound($1D_Array, 0) <> 1 Then Return SetError(1, 0, "") ; error on arrays dimensions
    ReDim $2D_Array[UBound($2D_Array) + 1][UBound($2D_Array, 2)] ; add 1 row to the 2D array
    For $i = 0 To UBound($1D_Array) - 1 ; loop all alements of the 1D array
        If $i < UBound($2D_Array, 2) Then ; if we are in boundaries then add element to the new row
            $2D_Array[UBound($2D_Array) - 1][$i] = $1D_Array[$i]
        Else ; if out of boundaries then do not more elements and set @extended
            SetError(0, 1)
            ExitLoop
        EndIf
    Next
EndFunc   ;==>__ArrayAdd_1DRow
Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

dromenox,

Just change the way $tmparray is defined like BrewManNH is suggesting...

#include <array.au3>

local $array[0][6]
local $tmparray=[[1,2,3]]   ;   <- just change def to 2D array
_arrayadd($array,$tmparray)
_arraydisplay($array)

kylomas

edit: spelling

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

I would suggest declaring the 2D array as $array[1][6] as opposed to $array[0][6],

Reason being is you would have to expand the array before actually assigning anything to it. (Not really an issue, But eh?)

Here's a solution (I've included my ExpandArray Function, As you may find it useful for your needs)
 

#include <array.au3>

local $temparray[6] = [0,4,5,6,7,8], $array[1][6], $anotherArray[4] = [1,2,3,4]

;Row Assignment
For $x=0 to UBound($AnotherArray) - 1 ;Main For Loop
    
      $array[$x][0] = $AnotherArray[$x]
         
         ;Column Assignment
         for $i = 1 to ubound($temparray) - 1 ; Inner For loop, Runs every time the Main for loop is run
            $array[$x][$i] = $temparray[$i]
         Next
         
      ExpandArray($Array,2) ;Custom Function to dynamically expand array in for loop
      
Next
_ArrayDisplay($Array)





; #FUNCTION# ====================================================================================================================
; Author ........: Javiwhite
; Modified.......: 09/07/2014
; $iArray........: The array that requires expanding
; $Dimensions....: The total number of dimensions in the array (Optional, Default is 1)
; $Update........: The dimension that you wish to expand (optional, Default is 1)
; $NewVal........: The new Upperbound of the dimension selected (optional, Default is current Ubound + 1)
; ===============================================================================================================================

func ExpandArray(ByRef $iArray,$dimensions = 1,$update=1,$newval=uBound($iArray)+1)

  if not IsArray($iArray) then return SetError(1,0,-1)

   if $dimensions < 1 then Return SetError(2,0,-1)
   local $tArray = $iArray

   If $dimensions == 1 Then
      ReDim $iArray[$newval]
      for $i = 1 to uBound($tArray) - 1
         if $i = uBound($iArray) then exitloop
         $iArray[$i] = $tArray[$i]
      Next
   ElseIf $dimensions == 2 Then
      if $update==1 Then
         ReDim $iArray[$newval][uBound($iArray,2)]
         for $i = 1 to uBound($tArray,1) - 1
            if $i = uBound($iArray,1) then ExitLoop
            for $x = 0 to uBound($tArray,2) - 1
               if $x = uBound($iArray,2) then ExitLoop
               $iArray[$i][$x] = $tArray[$i][$x]
            Next
         Next
      elseif $update==2 Then
         ReDim $iArray[uBound($iArray,1)][$newval]
         for $i = 1 to uBound($tArray,1) - 1
            for $x = 1 to uBound($tArray,2) - 1
               $iArray[$i][$x] = $tArray[$i][$x]
            Next
         Next
      EndIf
   ElseIf $dimensions == 3 Then
      if $update==1 Then
         ReDim $iArray[$newval][uBound($iArray,2)][uBound($iArray,3)]
         for $i = 1 to uBound($tArray,1) - 1
            for $x = 1 to uBound($tArray,2) - 1
               for $y = 1 to uBound($tArray,3) - 1
                  $iArray[$i][$x][$y] = $tArray[$i][$x][$y]
               Next
            Next
         Next
      ElseIf $update==2 Then
         ReDim $iArray[uBound($iArray,1)][$newval][uBound($iArray,3)]
         for $i = 1 to uBound($tArray,1) - 1
            for $x = 1 to uBound($tArray,2) - 1
               for $y = 1 to uBound($tArray,3) - 1
                  $iArray[$i][$x][$y] = $tArray[$i][$x][$y]
               Next
            Next
         Next
      ElseIf $update==3 Then
         ReDim $iArray[uBound($iArray,1)][uBound($iArray,2)][$newval]
         for $i = 1 to uBound($tArray,1) - 1
            for $x = 1 to uBound($tArray,2) - 1
               for $y = 1 to uBound($tArray,3) - 1
                  $iArray[$i][$x][$y] = $tArray[$i][$x][$y]
               Next
            Next
         Next
      EndIf
   Else
      Return SetError(3,0,-1)
   EndIf
   Return uBound($iArray)
EndFunc

I'm aware that the ExpandArray looks a little sloppy, I've been meaning to tidy it up, But I never really intended on sharing the function, So apologies for the needless if commands and lack of comments ;)

Hopefully this gets you on the right tracks.

Many Thanks

Javi


give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

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  
Followers 0