Jump to content

Recommended Posts

Posted

HI I have a program that reads a file that contains the servername and IP address separated by a comma

The program reads perfectly until the last line which is a blank and it generates an error message 

samplereadcsvwithcommas.au3" (26) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$value2 = $input[2]
$value2 = ^ ERROR
>Exit code: 1    Time: 60.44
 

Here is the code

#include <File.au3>
$txtfile = FileOpen(@WorkingDir & "\devices.csv", 0)
While 1
              $line = FileReadLine($txtfile)
            If @error = -1 Then ExitLoop
               filewrite("dummy.csv",$line)
             ;msgbox (0, "Line", $line,5)
             $string = FileReadLine("DUMMY.csv",1)
             $input = StringSplit($string, ",", 1)
             $value1 = $input[1]
             $value2 = $input[2]
             msgbox (0, "Name", $VALUE1,5)
             msgbox (0, "IP Address", $VALUE2,5)
               SLEEP(20)
             fileDELETE("dummy.csv")
             ;Endif

WEnd

Any ideas ?  Thank you everyone!

 

Chris

 

 

 

Posted

Use

ConsoleWrite(@Error)
_ArrayDisplay($input)

after StringSplit to check for errors and see the returned array.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

 

Posted

Add a check for empty line (empty string).

#include <File.au3>
$txtfile = FileOpen(@WorkingDir & "\devices.csv", 0)
While 1
    $line = FileReadLine($txtfile)
    If @error = -1 Then ExitLoop
    If $line = "" Then ExitLoop
    $input = StringSplit($line, ",", 1)
    $value1 = $input[1]
    $value2 = $input[2]
    MsgBox (0,"Result" ,"Name:" & $input[1] & @CRLF & "IP Address:" & $input[2], 5)
    SLEEP(20)
WEnd

 

Posted (edited)

Based on the return value described here: https://www.autoitscript.com/autoit3/docs/functions/StringSplit.htm

I would insert a check after StringSplit. It's a little more specific than checking for a blank line, as it could fail on non-blank inputs.

$string = FileReadLine("DUMMY.csv",1)
$input = StringSplit($string, ",", 1)
If $input[0] < 2 Then ExitLoop        ;   <-- check that StringSplit found (at least) two entries
$value1 = $input[1]
$value2 = $input[2]

 

Edited by quickbeam
formatting

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
×
×
  • Create New...