Page 1 of 1 1
Topic Options
#207905 - 2013-10-10 08:04 PM How to declare a Multi-dimensional Arrary ?
jenefa Offline
Fresh Scripter

Registered: 2013-02-01
Posts: 15
Loc: USA
Hello All,

Do i need to declare a Multi-dimensional Arrary ?

I need to store some information that i am reading from CSV to an array.

Please help.
RJ

Top
#207906 - 2013-10-10 08:26 PM Re: How to declare a Multi-dimensional Arrary ? [Re: jenefa]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Multidimensional Array is Dim $aData[x,y]

You might find using the CSV function and an "Array of Arrays" easier to use. For example:
 Code:
; Load CSV file into an array
$aFile = FileIO('.\file.csv', 'R')
; convert array of CSV Strings to Array of Arrays of fields
For EP = 0 to UBound($aFile)
  $aFile[$P] = CSV($aFile[$P]
Next
With an array of arrays in this manner, you can reference it via $aFile[$Record][$Field]. Note the double set of element identifiers.

The third field of the 27th record of the file can be accessed directly as $aFile[26][2] (recall that arrays are 0-based, so the third field is element 2).

One advantage of this method is that you only work with single-dimension arrays, and both the outer (records) and inner (fields) can be resized with ReDim.

This method takes a little thought the first time you use it.. just remember that the first index is the array of lines (records) in the file, and the second is the array of CSV values (fields) in each line.

Both FileIO() and CSV() can be downloaded from the resources/Kix UDF Library page on my web site.
Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#207908 - 2013-10-11 05:08 PM Re: How to declare a Multi-dimensional Arrary ? [Re: Glenn Barnas]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
I can only second what Glenn said .. begin with the Array of Arrays method (and maybe just stick to it)

The real Multidimensional Array [x,y] is a bit awkward to use. One thing comes to mind:
They may support up to 60 dimensions but if you redim and preserve the array you can only alter the 'rightmost' dimension (y)

As soon as you understood how [x][y] works you will love it..
_________________________



Top
#207918 - 2013-10-17 08:07 PM Re: How to declare a Multi-dimensional Arrary ? [Re: Jochen]
jenefa Offline
Fresh Scripter

Registered: 2013-02-01
Posts: 15
Loc: USA
So, does FileIO read the entire CSV file to an Array of Arrays or should i loop FileIO each i time i use CSV function.
Top
#207919 - 2013-10-17 08:52 PM Re: How to declare a Multi-dimensional Arrary ? [Re: jenefa]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
The FileIO UDF (must be downloaded and referenced) will read a text file into an array of LINES. The CSV UDF converts a line of text in CSV format to an array, or an array to a line of text in CSV format. Download both from the Kix UDF Library on my web site.

The For/Next loop parses each record in the file data array, converting it from a CSV format to an array, and then storing the array back into the file array.

The result is an array of arrays - $aFile is an array of records in the file, $aFile[3] contains an array of fields from the CSV data that was converted.

So - if you want to examine the third field of the 12th record, you would reference $aFile[11][2] (recall that arrays are 0-based, so the first record is element 0, the third is element 2).

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#207921 - 2013-10-17 10:59 PM Re: How to declare a Multi-dimensional Arrary ? [Re: Glenn Barnas]
jenefa Offline
Fresh Scripter

Registered: 2013-02-01
Posts: 15
Loc: USA
Thanks Glenn.
I think i got it working now.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.072 seconds in which 0.04 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org