Arrays - Good Description
Keywords: arrays
Question:
I have seen a couple of questions regarding arrays, read all the posts and so on, I get the feeling the other
people who posted may be as lost as I am concerning 2 or three dimensional arrays. There are a couple of
posts that get into them some, but seem to focus more on the udf's rather than the arrays. Could you possibly
put a very small example out here on a 2 or 3 dimensional array for the lost souls like myself?
Answer:
data=ArrDimension(10,2)
;fill array
for xx=0 to 9
data[xx,0] = xx+xx
data[xx,1] = xx*xx
next
;report on results
for xx=0 to 9
xxplus=data[xx,0]
xxmult=data[xx,1]
Pause(strcat("xx is ",xx),strcat("xx+xx is ",xxplus,@crlf,"xx*xx is ",xxmult))
next
I think where most people tend to get stuck is where the data goes. In a two-dimensional array, the first
index doesn't contain data, it merely acts as a pointer to the data in the second dimension. (To take it further,
only the nth dimension has data, the other dimensions act as pointers to this data.)
I tend to think of it more as a series of records:
eg.
arr=arrdimension(2,3)
This gives me an array of two records, each of these records has three elements (eg. the three might be
name, address, postcode or something like that). So, to access the data, I choose which record I want out of
the two, and grab the data:
name=arr[0,0]
addr=arr[0,1]
pc=arr[0,2]
Hope that helps.
Question (cont'd)
I really do appreciate this, cause I can be real thick headed sometimes. Getting the data out seems a little
clearer. If cars.dat was as follows:
chevy truck red
chevy truck green
dodge car green
ford car blue
I believe the following code would return green:
array=ArrDimension(4,3)
myans = array[1,3]
message("info", myans)
exit
I would test it, but that leads to my next question: How do you fill the array with the info from cars.dat?
Answer:
chevy truck red
chevy truck green
dodge car green
ford car blue
;assuming that these values are in a file cars.dat
fn="cars.dat"
handle=FileOpen(fn,"READ")
mycars=ArrDimension(100,3)
index=0
imake=0
itype=1
icolor=2
while 1
line=StrTrim(FileRead(handle))
if line=="*EOF*" then break
if line=="" then continue
mycars[index,imake]=ItemExtract(1,line," ")
mycars[index,itype]=ItemExtract(2,line," ")
mycars[index,icolor]=ItemExtract(3,line," ")
index=index+1
endwhile
FileClose(handle)
;index is now a count of elements in the array
;compute maxindex. Since array is zero based,
;just subtract 1
maxindex = index -1
;look at results
for index=0 to maxindex
Message(index,strcat(mycars[index,imake]," ",mycars[index,itype]," ",mycars[index,icolor]))
next
I always think of a 2 dimentional array being like a spreadsheet:
Name SSN Income
Jeff 123-45-6789 10000
Marty 234-56-7890 100000
Laura 345-67-8901 50000
a(1,1) = "Jeff"
a(1,2) = "123-45-6789"
a(1,3) = "10000"
a(2,1) = "Marty"
a(2,2) = "234-56-7890"
etc...
A 3 dimentional array would be like a workbook in Excel. It might look like this:
Price(Year, Month, Region)
Article ID: W14820