Jump to content

Reading lines into a multi-dimension array


Recommended Posts

I've got a CSV file I'm trying to read in. Each line in the file should be a separate entry in the array such that the sample file of

20,1,2,3,4,5,6,7,8
30,1,2,3,4,5,6,7,8
40,1,2,3,4,5,6,7,8

should be a 3x9 array and "look" like this:

[(20,1,2,3,4,5,6,7,8)
(30,1,2,3,4,5,6,7,8)
(40,1,2,3,4,5,6,7,8)]

My code is as follows:

dim $testArray[1][9]
while 1
  $testCase = FileReadLine($testFile)
  if @error = -1 then ExitLoop
  $testArray[$i] = StringSplit($testCase, ",")
  $i = $i + 1     ;Increment $i
  redim $testArray[$i][9]
WEnd

I keep getting this error:

Main.au3 (188) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
$testArray[$i] = StringSplit($testCase, ",")
^ ERROR
->14:14:39 AutoIT3.exe ended.rc:1

No matter what variable I change in the $testarray[1][9] bit, it doesn't work, even if I remove the redim and only read in 1 line.

Would appreciate some help on this

Link to comment
Share on other sites

Please check this You'll find a function to read a CSV file into a 2D-array.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I followed both of the above links and sort of followed what was going on, but found it a bit confusing, so I've attempted my own thing and it sort of works:

$dummyArray = StringSplit($testCase, ",")
  for $j = 1 to $j = 9 step 1
   MsgBox(0,"loopy","entered j")
   $testArray[$i][$j] = $dummyArray[$j]
  Next

It compiles fine. New problem though. If the code is as it is above, it never enters the for loop. If I change it to be "For $j = 0 to..." then it throws up the same incorrect number of subscripts error, regardless of what I change any $j values you (i.e. $testArray[$i][($j+1)] etc)

Link to comment
Share on other sites

Threw in some _ArrayDisplay() to check the output.

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

Global $file = @DesktopDir & "/test.txt", $iLineCount = _FileCountLines($file), $aiOutput[$iLineCount][999], $asReadLines
_FileReadToArray($file, $asReadLines)
_ArrayDisplay($asReadLines)
$x = -1
$iMaxLines = 0
For $line in $asReadLines
    If $x = -1 Then
        $x+=1
    Else
        $aiDelimited = StringSplit($line, ",", 0)
        _ArrayDisplay($aiDelimited)
        If $aiDelimited[0] > $iMaxLines Then $iMaxLines = $aiDelimited[0]
        For $i = 1 to $aiDelimited[0]
            $aiOutput[$x][$i - 1] = $aiDelimited[$i]
        Next
        $x+=1
    EndIf
Next
ReDim $aiOutput[$iLineCount][$iMaxLines]
_ArrayDisplay($aiOutput)
Edited by mechaflash213
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Well, when I use this code with Text.csv as a text file with the content from post#1

#include <array.au3>
#include "StringSplitW.au3"

Global $csv = FileRead("Text.csv")
$aS = StringSplitW($csv, ",")
_ArrayDisplay($aS)

I will get the 2d array appropriately.

StringSplitW.au3 is from the link I posted (For 2D arrays:)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Well, when I use this code with Text.csv as a text file with the content from post#1

#include  #include "StringSplitW.au3" Global $csv = FileRead("Text.csv") $aS = StringSplitW($csv, ",") _ArrayDisplay($aS)
I will get the 2d array appropriately. StringSplitW.au3 is from the link I posted (For 2D arrays:) Br, UEZ
I'm claiming native support ;)
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

I managed to get one of the modules I was linked to work. It's more or less what I was trying to do, but with a naming convention I don't like. Half tempted to try and change things, but probably won't

Thanks for the help ;)

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