kasperghost

arrays 2d help plz

9 posts in this topic

for the love of god i cant figure out what i'm doing wrong in the following code when trying to change value of a column in a 2d array

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.2
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

$spacechar = chr(32)
$aArray = 0

_FileReadToArray(@ScriptFullPath, $aArray)

Global $bArray[$aArray[0] + 1]

for $i = 1 to $aArray[0]
    $bArray[$i] = StringSplit($aArray[$i], $spacechar)
Next

;for $i = 1 to $bArray[0]
;   for $b = 1 to $bArray[$i][0]
;       $bArray[$i][$b] = StringStripWS($bArray[$i][$b], 8)
;   Next
;Next

_ArrayDisplay($bArray[4])
$bArray[4][11] = "ok"
_ArrayDisplay($bArray[4])

at the first arraydisplay we see that the 4th array has 11 rows

so i'm trying to change that row from that value to value ok

and then arraydisplay again to see if changed...

and eventually when that works write a function to remove empty/whitespaced columns from that 2d array...

but first things first why this code always fails with

new1.au3" (34) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
$bArray[4][11] = "ok"
^ ERROR

 

if someone has any clue plz help.

thanks

simon

Share this post


Link to post
Share on other sites



Because you cannot access an array within an array like that.

In order to change the contents of an array in another array, you first have to extract the inner array to a temp array, change desired value, and put it back.

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

You can use a particular syntax to read an element but as said JO not to change it

#include <Array.au3>

$spacechar = chr(32)
Global $aArray = ["Hello word", "Nice play"]
_ArrayDisplay($aArray)

Global $bArray[UBound($aArray)]
for $i = 0 to UBound($aArray)-1
    $bArray[$i] = StringSplit($aArray[$i], $spacechar)
Next
_ArrayDisplay($bArray[1])

Msgbox(0,"", ($bArray[1])[2] )  ; works for reading
($bArray[1])[2]  = "game"
Msgbox(0,"", ($bArray[1])[2] )  ; doesn't work for writing

$tmp = $bArray[1]   ; as saith JO
$tmp[2] = "game"
$bArray[1] = $tmp
_ArrayDisplay($bArray[1])

 

Share this post


Link to post
Share on other sites

so in this case where we have a 2d array are there any examples on how to convert that 2d array into many 1d arrays where i can manipulate them

something like arr1, arr2, arr3 for the n number of arrays inside that array? since i cant pre-create in advance arrays, since i wont know how many arrays need to pre-create..

my goal is to read a text file with several lines of text and i want to be able to separate by line and then each line by word

all in the file are separated by spaces which are different per line for example
abc abc    abc    abc abc
abc      abc  abc bac

etc..

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Yes, there will be ways.

First, can you tell you actual endgame, because you may well be wasting a lot of time doing stuff like this, if there is an easier way.

I get that you want each space separated word in an array for each line in a file. But for what purpose?

You're are under no obligation to answer and I don't normally ask, but it would help your cause.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

well those files i'm trying to read are kinda like compiler scripts
for a custom hardware automation system. I want to build a small program
that will be something like an editor for those files.
and allow you to easily either add lines either modify existing lines
based on some criteria, so that the final script do more functionality.
Editing them by hand is time consuming since there are like hundrends
if not almost a thousand lines. I dont know why they builded it
space separated without some format like xml or ini or whatever.
Only the number of spaces is defined if this line is supposed to cover a certain function
but next line if it is for another function then it has different number of spaces because it has more values.
Since the original developer/company no longer exists I want to build a
small editor to edit those files since I have many of those in many workstations.
Maybe it would be best to search for code for csv files parser and modify to my needs?

 

And yes as you guessed when I found a way to parse them I will actually count each space between values to figure out a pattern so that I can add the lines... LOL XD

Edited by kasperghost
extra info...

Share this post


Link to post
Share on other sites

Can you post an example of the real data?


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

#9 ·  Posted (edited)

kasperghost,

Crank your file through this...

#include <StringConstants.au3>
#include <array.au3>

; read your file to a 1D array splitting at EOLS (assuming CRLF for EOL string)

local $aFile = stringsplit(fileread(@scriptdir & '\1.txt'),@CRLF,  $STR_NOCOUNT + $STR_ENTIRESPLIT)
_arraydisplay($aFile,'Each line is an array element')

; determine max number of arguments to dimension 2D array

local $iMaxCols

for $i = 0 to ubound($aFile) - 1
    stringreplace($aFile[$i],' ',' ')
    $iMaxCols = (@extended > $iMaxCols ? @extended : $iMaxCols)
Next

; create the result array as $aResult[# of lines from file][Max # of spaces + 1]

local $aResult[ubound($aFile)][$iMaxCols+2]

; populate the 2D array...col 0 is the number of parms/stmts/args for the line

local $aTmp

for $i = 0 to ubound($aFile) - 1
    $aTmp = stringsplit($aFile[$i],' ')
    for $j = 0 to ubound($aTmp) - 1
        $aResult[$i][$j] = $aTmp[$j]
    Next
Next

_arraydisplay($aResult,'# of arguments per line is in col0')

maybe it will help.

kylomas

edit: Important - the above code assumes that space is used only as a delimiter and values have no embedded spaces.  If there are embedded spaces then we can use a regular expression solution for the 2ND split.

Edited by kylomas
additional info

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