Sign in to follow this  
Followers 0
TomA

Building a Multi-Dimensional Array

5 posts in this topic

I am trying to build a multi-dimenional array (2 in this case). However, I am trying to it dynamically and loading it from data read in from a file.

Dim $RegionArray ; define array itself - definition 1

Global $RegionArray ; definition 2 - prime the intial iteration, and drop it later

$RegionArray [0][0] = ""

$RegionArray [0][1] = ""

;now comes the problem

Func buildRegionArray(ByRef $RegionArray)

Local $regionFileLoc = "c:\dat\Region_IDs.txt"

Local $regionFile = FileOpen($regionFileLoc, 0)

Local $rLine

local $rLen

Local $state

Local $codes

Local $ix = 0

while 1

$rLine = FileReadLine($regionFile)

if @error = -1 then ExitLoop

$rLen = StringLen($rLine)

$state = StringLeft($rLine, 2)

$codes = StringRight($rLine, $rLen - 4)

;$RegionArray[$ix][0] = $state

;$RegionArray[$ix][1] = $codes

;$ix += 1

;_ArrayAdd($RegionArray, $state, $codes)

WEnd

FileClose($regionFile)

EndFunc

; I get an error using either of the two above commented out methods.

; method one would bild the array based on an index, ie, [0][0], [0][1], etc. by increasing $ix with each iteration.

; the second method would be used with definition 2 of the array. however, _ArrayAdd does not handle multi-dimenional arrays

The only other way that I can possible do this would be to read the file first and find out how many records there are. Then

define it using a variable, with I don't think would work either.

IE, If file had 35 records, Global $array[$noRecs][2], indicating that there would be 35 rows, of two columns each.

Anyone have an idea or done this themselves?

TIA, Tom

:whistle:

Share this post


Link to post
Share on other sites



I am trying to build a multi-dimenional array (2 in this case). However, I am trying to it dynamically and loading it from data read in from a file.

Dim $RegionArray ; define array itself - definition 1

Global $RegionArray ; definition 2 - prime the intial iteration, and drop it later

$RegionArray [0][0] = ""

$RegionArray [0][1] = ""

;now comes the problem

Func buildRegionArray(ByRef $RegionArray)

Local $regionFileLoc = "c:\dat\Region_IDs.txt"

Local $regionFile = FileOpen($regionFileLoc, 0)

Local $rLine

local $rLen

Local $state

Local $codes

Local $ix = 0

while 1

$rLine = FileReadLine($regionFile)

if @error = -1 then ExitLoop

$rLen = StringLen($rLine)

$state = StringLeft($rLine, 2)

$codes = StringRight($rLine, $rLen - 4)

;$RegionArray[$ix][0] = $state

;$RegionArray[$ix][1] = $codes

;$ix += 1

;_ArrayAdd($RegionArray, $state, $codes)

WEnd

FileClose($regionFile)

EndFunc

; I get an error using either of the two above commented out methods.

; method one would bild the array based on an index, ie, [0][0], [0][1], etc. by increasing $ix with each iteration.

; the second method would be used with definition 2 of the array. however, _ArrayAdd does not handle multi-dimenional arrays

The only other way that I can possible do this would be to read the file first and find out how many records there are. Then

define it using a variable, with I don't think would work either.

IE, If file had 35 records, Global $array[$noRecs][2], indicating that there would be 35 rows, of two columns each.

Anyone have an idea or done this themselves?

TIA, Tom

:whistle:

Update, defining the array after reading in the file using a variable does work and as long as it is defined as Global, it is available anywhere, as long as it is build first.

Share this post


Link to post
Share on other sites

I hate to say it, but everything you said makes very little sense. OK, not everything but enough of it to make it hard to understand what you are doing, and what isn't working. Based on what I THINK you want, here is a suggestion

dim $RegionArray[2][2]
$RegionArray[0][0]=1

_BuildRegionArray($RegionArray)
local $DisplayArray
for $i = 1 to $RegionArray[0][0]
$DisplayArray &= $RegionArray[$i][0] & @tab & $RegionArray[$i][1] & @crlf
next
msgbox(0,"",$DisplayArray)

Func _BuildRegionArray(ByRef $RArray)
Local $regionFileLoc = "c:\dat\Region_IDs.txt"
Local $regionFile = FileOpen($regionFileLoc, 0)
Local $rLine, $rLen, $state, $codes

while 1
$rLine = FileReadLine($regionFile)
if @error = -1 then ExitLoop
$RArray[0][0]+=1
Redim $RArray[$RArray[0][0]][2]

$rLen = StringLen($rLine)
$state = StringLeft($rLine, 2)
$codes = StringRight($rLine, $rLen - 4)

$RArray[$RArray[0][0]-1][0] = $state
$RArray[$RArray[0][0]-1][1] = $codes

WEnd
$RArray[0][0] -=1

FileClose($regionFile)

EndFuncoÝ÷ Ù8^¥ªíjëh×6local $DisplayArray
for $i = 1 to $RegionArray[0][0]
$DisplayArray &= $RegionArray[$i][0] & @tab & $RegionArray[$i][1] & @crlf
next
msgbox(0,"",$DisplayArray)

is simply to display the results, and can be removed. everything else SHOULD work, but I have to reiterate that I am not 100% sure what you want. Finally, this code isn;t tested as I am not at home right now. If there are any bugs, post back here and I should be able to get them out quickly

Share this post


Link to post
Share on other sites

Did a cut and paste and voila, it works like a charm.

I am relatively new (3 weeks or so) to autoit, so the Redim is not yet something that I have done anything with. Very nice solution.

Thanks, T

Share this post


Link to post
Share on other sites

Now that I'm home, I would say that a better way of doing this would be along these lines...

dim $RegionArray=_BuildRegionArray("c:\dat\Region_IDs.txt")

local $DisplayArray
for $i = 1 to $RegionArray[0][0]
$DisplayArray &= $RegionArray[$i][0] & @tab & $RegionArray[$i][1] & @crlf
next
msgbox(0,"",$DisplayArray)

Func _BuildRegionArray($FilePathFull,$l=2,$tl=4)
Local $regionFile = FileOpen($FilePathFull, 0)
local $RArray[2][2]

while 1
local $rLine = FileReadLine($regionFile)
if @error = -1 then ExitLoop
$RArray[0][0]+=1
Redim $RArray[$RArray[0][0]+1][2]

$RArray[$RArray[0][0]-1][0] = StringLeft($rLine, $l)
$RArray[$RArray[0][0]-1][1] = StringTrimLeft($rline, $tl)

WEnd

FileClose($regionFile)
return $RArray

EndFunc

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