Jump to content

Multi dimensional array from file


Recommended Posts

I have used FileReadToArray in the past for one dimensional arrays, this time however the file contains multiple data elements. I need to be able to read a text file that contains 3 fields (PC name, IP Address, Port Number) into a multi-dimensional array.

The file is currently tab delimited, but that could be changed if needed. Can anyone provide any guidance on the best way to accomplish this?

Link to comment
Share on other sites

- Declare the big array to hold your results

- read text file using _FileReadToArray

- split every element from your array using the delimiter of your choice and populate your "big" array elements

Easy enough ;)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Hi Erik,

You can use nested loop.

#include <Array.au3> ; for _ArrayDisplay

Local $array [3][3] ; Fixed size 2d array. Use ReDim to expand array.

$data = FileRead("data.txt")

$row = StringSplit($data,@CRLF,3) ; flag 1 + 2. For more information, refer to the help file.

For $i = 0 To UBound($row)-1
    $col = StringSplit($row[$i],",",2)
    For $j = 0 To UBound($col)-1
        $array[$i][$j] = $col[$j]
    Next
Next

_ArrayDisplay($array)

data.txt ==>

x1,x2,x3
y1,y2,y3
z1,z2,z3

Hi ;)

Link to comment
Share on other sites

Thanks for all the advice. I ended up piecing code together that I found from several examples.

This is what I came up with...

#include <file.au3>
#include <array.au3>

Dim $aRows
If Not _FileReadToArray("TTYID.txt", $aRows) Then
    MsgBox(4096, "Error", " Error reading file to Array     error:" & @error)
    Exit
EndIf

Global $myTTYID[$aRows[0]][3]
For $r = 1 To $aRows[0]
    $aCols = StringSplit($aRows[$r], @TAB, 1)
    For $c = 1 To $aCols[0]
        $myTTYID[$r - 1][$c - 1] = $aCols[$c]
    Next
Next

_ArrayDisplay($myTTYID, "$myTTYID")

I don't know if it was the best or the most simple way, but it works...

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...