Sign in to follow this  
Followers 0
jsteng

Problem with 2-dimensional Array

12 posts in this topic

#1 ·  Posted (edited)

GLOBAL $TimeOut[8][8],$Text[8]
$Text[0] = "1000,2000,3000"
$Text[1] = "1000,2000,3000"
$Text[2] = "1000,2000,3000"
$Text[3] = "1000,2000,3000"
$Text[4] = "1000,2000,3000"
$Text[5] = "1000,2000,3000"
$Text[6] = "1000,2000,3000"
$Text[7] = "1000,2000,3000"

for $i = 0 to 7
    $TimeOut[$i] = StringSplit( $Text[$i],",",2)
next

Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

 

How do I assign an Array into an Array using StringSplit?

Edited by jsteng

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You changed the code you posted. Anyway pseudo code in and you get pseudo code out. The principle is the same though.

CODE DELETED


Now for your modified question, here's the working solution.

GLOBAL $TimeOut[8][8], $Text[8], $aTemp
$Text[0] = "1000,2000,3000"
$Text[1] = "1000,2000,3000"
$Text[2] = "1000,2000,3000"
$Text[3] = "1000,2000,3000"
$Text[4] = "1000,2000,3000"
$Text[5] = "1000,2000,3000"
$Text[6] = "1000,2000,3000"
$Text[7] = "1000,2000,3000"

for $i = 0 to 7
    $aTemp = StringSplit( $Text[$i], "," ,2)
    For $j = 0 To 2
        $TimeOut[$i][$j] = $aTemp[$j]
    Next
next

;

Notice you have to give both row and column references with a two dimensional array ==> $aArray[$iRow][$iCol] and use nested loops to access individual elements within it.

Edited by czardas

Share this post


Link to post
Share on other sites

I thought I can "wholesale" assign an array into an array to make it 2D

Now i understand.

many thanks!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

GLOBAL $TimeOut[8][8], $Text[8], $aTemp
$Text[0] = "1000,2000,3000"
$Text[1] = "1000,2000,3000"
$Text[2] = "1000,2000,3000"
$Text[3] = "1000,2000,3000"
$Text[4] = "1000,2000,3000"
$Text[5] = "1000,2000,3000"
$Text[6] = "1000,2000,3000"
$Text[7] = "1000,2000,3000"

for $i = 0 to 7
    $aTemp = StringSplit( $Text[$i], "," ,2)
    For $j = 0 To 2
        $TimeOut[$i][$j] = $aTemp[$j]
    Next
next

;Now how do I do something like this?
_ArrayPush($TimeOut[0],$TimeOut[0][0],0) 
;

I am getting this error with _ArrayPush
Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

I can't do ArrayPush on multi dimensional arrays?

(My purpose for the arrayPush is to rotate the data)

Is it possible to pass the reference of the 2nd dimension array to a function?

If so, How?

Edited by jsteng

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You can't do that. First --as the help file says-- the array must be 1D. Then AutoIt unfortunately doesn't support partial indexing on multi-dimensional arrays, so $TimeOut[0] is causing the error you get.

Use an ad hoc loop instead.

.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

The more I try to solve my multi-dimensional array problem, the more problems I encounter.

GLOBAL $TimeOut[8][8], $Text[8], $aTemp
$Text[0] = "0"
$Text[1] = "1000,3000"
$Text[2] = "1000,2000,3000"
$Text[3] = "3000"
$Text[4] = "2000,3000"
$Text[5] = "1000,2000,3000,4000,5000,6000"
$Text[6] = "0"
$Text[7] = "1000"

for $i = 0 to 7
    $aTemp = StringSplit( $Text[$i], "," ,2)
    For $j = 0 To 2
        $TimeOut[$i][$j] = $aTemp[$j]
    Next
next



;Simulated _ArrayPush($TimeOut[0],$TimeOut[0][0],0)
local $firstItem = $TimeOut[0][0]
local $lastIndex = uBound($TimeOut[0])-1
for $i=0 to $lastIndex-1
  $TimeOut[0][$i] = $TimeOut[0][$i+1]
next
$TimeOut[0][$lastIndex] = $firstItem

UBOUND will not allow me to check on a multi-dim array...
 It actually does, but the way I am using above, with the 2nd dimension having different length this will not work:

UBOUND($TimeOut,2)

Seems that I should just put the data AS a string into the array, and have a lengthly/slower code that will convert it to an array, rotate, put it back as a string...

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

jchd is correct. You don't rotate the array: Instead you rotate the calling sequence. Here's a simple example!

;

GLOBAL $TimeOut[8][8], $Text[8], $aTemp
$Text[0] = "0"
$Text[1] = "1000,3000"
$Text[2] = "1000,2000,3000"
$Text[3] = "3000"
$Text[4] = "2000,3000"
$Text[5] = "1000,2000,3000,4000,5000,6000"
$Text[6] = "0"
$Text[7] = "1000"

for $i = 0 to 7 ; Transform to a 2D array.
    $aTemp = StringSplit( $Text[$i], "," ,2)
    For $j = 0 To UBound($aTemp) -1
        $TimeOut[$i][$j] = $aTemp[$j]
    Next
next

Local $iNextRow
For $i = 0 to 99
    $iNextRow = Mod($i, UBound($TimeOut)) ; Round and round we go
    ConsoleWrite($TimeOut[$iNextRow][0]) ; This could go inside the next loop using ==> For $j = 0 To 7
    For $j = 1 To 7
        If $TimeOut[$iNextRow][$j] = "" Then ExitLoop ; This comparison may not be suitable in different circumstances.
        ConsoleWrite("," & $TimeOut[$iNextRow][$j]) ; Comma is added back for this demonstration only.
    Next
    ConsoleWrite(@CRLF) ; Next row
Next

;

You can overwrite elements as you go to emulate an _ArrayPush, always keeping tabs on which row you want to treat as index 0 (virtual first row). There are many ways to do something like this.

Edited by czardas

Share this post


Link to post
Share on other sites

In addition to what czardas just posted, you can differentiate empty cells from those containing "0".

Anyway $TimeOut[0] has no meaning, as in uBound($TimeOut[0])-1. AutoIt has no syntax for refering to a row or a column of a 2D (or higher dimensional) array. Either refer to the array as a whole or an individual element.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Curious: Is there an autoit equivalent to Perl's @{$TimeOut[0]}?

Or a means of getting the pointer to the 2nd dimension?

That way, i can now do uBound( @{$TimeOut[0]} )

Share this post


Link to post
Share on other sites

You want to know the Ubound of the second dimension?

From the Helpfile:

UBound ( Array [, Dimension = 1] )

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

A good alternative approach is to set the first column as a row entries counter like this:

;

#include <Array.au3>

Global $TimeOut[8][9], $Text[8], $aTemp
$Text[0] = "0"
$Text[1] = "1000,3000"
$Text[2] = "1000,2000,3000"
$Text[3] = "3000"
$Text[4] = "2000,3000"
$Text[5] = "1000,2000,3000,4000,5000,6000"
$Text[6] = "0"
$Text[7] = "1000"

for $i = 0 to 7 ; Transform to a 2D array.
    $aTemp = StringSplit( $Text[$i], ",") ; StringSplit default flag (=0).
    For $j = 0 To $aTemp[0]
        $TimeOut[$i][$j] = $aTemp[$j]
    Next
next

_ArrayDisplay($TimeOut) ; First column gives the number of entries in each row.
Edited by czardas

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