Sign in to follow this  
Followers 0
Arterie

Table in array

8 posts in this topic

#1 ·  Posted (edited)

I want to read a table of data into an array... e.g.

name| peter klaus

chars| 5 5

But i want to assign it "automaticly". That means i do not want to write :

$array[0][0] = "peter"

Maybe thers a way to read a tabloe out of a *.txt file or out of the script like you can do in C++ like:

$array =

{

peter klaus

5 5

}

I hope you know what i mean :)

Edited by Arterie

Share this post


Link to post
Share on other sites



_filereadtoarray


Kerros===============================================================How to learn scripting: Figure out enough to be dangerous, then ask for assistance.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

hm... I got a *.txt file like this:

w.w.w.w.w
w.0.0.0.w
w.0.1.0.w
w.0.0.0.w
w.w.w.w.w

And i want that to be read into an array...so that it offers 1 if i ask for

$array[2][2]

I already tried with _FileReadToArray but then this are just lines and this isnt what i want....

I thought of something like reading out,then using stringsplit and then copieing this into another array...but i dont get it

Edited by Arterie

Share this post


Link to post
Share on other sites

#include <File.au3>
#include <Array.au3>

Global $sFile, $aFile, $aTable[1][1], $aTemp, $i, $j

$sFile = "test.txt"
_FileReadToArray($sFile, $aFile)
If @error = 1 Then Exit

For $i=1 To $aFile[0]
    $aTemp = StringSplit($aFile[$i], ".")
    If @error <> 1 Then
        ReDim $aTable[$i][$aTemp[0]]
        For $j=1 To $aTemp[0]
            $aTable[$i-1][$j-1] = $aTemp[$j]
        Next
    EndIf
Next

_ArrayDisplay($aTable)

Share this post


Link to post
Share on other sites

Put the contents you spoke of in a file called test.txt and run the following... Is this what you mean?

(By the way, I made it work for any gridsize as long as it's in the exact format as your example, meaning all lines have equal amount of elements, and no non-zero lines are used.)

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

Dim $aRecords[1], $tempArray[1], $array[1][1]
_FileReadToArray("c:\test.txt", $aRecords)
$max_y = $aRecords[0] - 1

For $y = 0 To $max_y
 $tempArray = StringSplit($aRecords[$y + 1], ".") ; <- aRecords's info that we need is records 1-5
 If $y = 0 Then
  $max_x = $tempArray[0] - 1
  ReDim $array[$max_x+1][$max_y+1]
 EndIf
 For $x = 0 To $max_x
  $array[$x][$y] = $tempArray[$x + 1] ; <- We also need $tempArray elements 1-5
 Next
Next

_ArrayDisplay($array,-1,-1,-1)

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Wow, that's almost exactly the same :)

Only I thought about really big arrays which would make the ReDim possibly slow :P My solution came with the drawback however of requiring a stricter format :P


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

True. You only need to run ReDim once, I was just in a hurry and didn't really care about efficiency.

Share this post


Link to post
Share on other sites

Thats great...Thank you

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