# FileRead 2 2D Array

## Recommended Posts

I'm trying to FileRead to a 2D array

9,1,9,0,0,0,6,0,0,0,0,0,0,0

8,1,5,0,0,0,0,7,0,0,0,0,0,0

7,10,9,0,0,0,0,0,9,0,0,0,0,0

6,6,9,0,0,0,77,0,0,0,0,0,0,0

5,1,34,0,0,0,19,0,0,0,0,0,0,0

4,1,9,66,0,0,0,0,0,0,0,0,0,0

3,34,9,0,0,0,0,0,0,0,0,0,0,0

2,1,9,0,0,0,0,0,0,0,0,0,0,0

How do I read this to a 2D array, so that each digit occupies a unique cell?

#include <Array.au3>
#include <File.au3>
Dim $array$file = FileOpen("C:\text.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in 1 character at a time until the EOF is reached While 1$chars = FileRead($file) If @error = -1 Then ExitLoop MsgBox(0, "Char read:",$chars, 1)
Wend

$array = StringSplit($chars, chr(44))
_ArrayDisplay($array) FileClose($file)

Does not quite cut it...

##### Share on other sites

I'm trying to FileRead to a 2D array

How do I read this to a 2D array, so that each digit occupies a unique cell?

#include <Array.au3>
#include <File.au3>
Dim $array$file = FileOpen("C:\text.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in 1 character at a time until the EOF is reached While 1$chars = FileRead($file) If @error = -1 Then ExitLoop MsgBox(0, "Char read:",$chars, 1)
Wend

$array = StringSplit($chars, chr(44))
_ArrayDisplay($array) FileClose($file)

Does not quite cut it...

This is the basic idea of one way. Needs more but as I say, just to show the idea.

$ft = fileopen("C:\text.txt", 0) dim$array[8][14];8 sets of 14
$p = 1;line count for$m = 0 to 7;for each line
$t = FileReadLine($ft)
$s =StringSplit($t,',')
for $n = 0 to$s[0] - 1
$Array[$m][$n] =$s[$n + 1] ConsoleWrite($Array[$m][$n] & ', ')
$p += 1 Next ConsoleWrite(@LF) Next FileClose($ft)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

##### Share on other sites

Cool, thanks.

I'll get the idea.

##### Share on other sites

I got it working...

#include <Array.au3>
#include <File.au3>
Dim $Array [8][14]$ft = FileOpen("C:\file.txt", 0)

; Check if file opened for reading OK
If $ft = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf$p = 1   ;line count
for $m = 0 to 7 ;for each line$t = FileReadLine($ft,$p)
$s =StringSplit($t,',')
For $n = 0 to$s[0] -1
$Array[$m][$n] =$s[$n + 1] Next$p += 1
ConsoleWrite(@LF)
Next
_ArrayDisplay($Array) FileClose($ft)

##### Share on other sites

I got it working...

#include <Array.au3>
#include <File.au3>
Dim $Array [8][14]$ft = FileOpen("C:\file.txt", 0)

; Check if file opened for reading OK
If $ft = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf$p = 1   ;line count
for $m = 0 to 7 ;for each line$t = FileReadLine($ft,$p)
$s =StringSplit($t,',')
For $n = 0 to$s[0] -1
$Array[$m][$n] =$s[$n + 1] Next$p += 1
ConsoleWrite(@LF)
Next
_ArrayDisplay($Array) FileClose($ft)
That's good, _arraydisplay isbetter than all those conwrites.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

## 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

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...