Jump to content
Skeletor

Array variable has incorrect number of subscripts

Recommended Posts

Hi Virtual People,

My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

 

For $count = 1 To _FileCountLines($FileRead1) Step 1
    $string = FileReadLine($FileRead1, $count)
    $input = StringSplit($string, ",", 1)
    $value1 = $input[1]
    $value2 = $input[2]
    $value3 = $input[3]

    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1")
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1")
    _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1")
Next

 

Share this post


Link to post
Share on other sites

Try that : 

 

Local $MyVar[3] = ["element 1", "element 2", "element 3"]


MsgBox (0, "", $MyVar)
MsgBox (0, "", $MyVar[0])
MsgBox (0, "", $MyVar[1])
MsgBox (0, "", $MyVar[2]) ;This is equal to $iRow amount (3) - 1 = 2 

$iRow = UBound ($MyVar)
MsgBox (0, "", $iRow)

If $iRow > 3 Then ; With that you dodged the fact to trying to access an non accessible $Variable. in this case it was $MyVar[3]
    MsgBox (0, "", $MyVar[$iRow-4]);$MyVar[3]
    MsgBox (0, "", $MyVar[$iRow-3]);$MyVar[2]
    MsgBox (0, "", $MyVar[$iRow-2]);$MyVar[1]
    MsgBox (0, "", $MyVar[$iRow-1]);$MyVar[0]
EndIf
If $iRow > 2 Then 
    MsgBox (0, "", $MyVar[$iRow-3]);$MyVar[2]
    MsgBox (0, "", $MyVar[$iRow-2]);$MyVar[1]
    MsgBox (0, "", $MyVar[$iRow-1]);$MyVar[0]
EndIf
If $iRow > 1 Then
    MsgBox (0, "", $MyVar[$iRow-2]);$MyVar[1]
    MsgBox (0, "", $MyVar[$iRow-1]);$MyVar[0]
EndIF
If $iRow > 0 Then
    MsgBox (0, "", $MyVar[$iRow-1]) ;$MyVar[0]
EndIf

 

Read help file about : 

UBound
IsArray

 

Edited by caramen

My video tutorials : HERE ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

@Skeletor
If you allow me, IsArray() doesn't tell you if you have less elements in your array and/or you array has some blank elements.
So, if you want to see if your array has less elements than the "normal", or has some blank elements, then you should do something like this:

#include <Array.au3>

Global $strIsArray = "Element1,Element2,ElementN", _
       $strLessElements = "Element1,Element2", _
       $strBlankElement = "Element1,,ElementN", _
       $arrIsArray = StringSplit($strIsArray, ",", $STR_NOCOUNT), _
       $arrLessElements = StringSplit($strLessElements, ",", $STR_NOCOUNT), _
       $arrBlankElements = StringSplit($strBlankElement, ",", $STR_NOCOUNT), _
       $varNotAnArray

_ArrayDisplay($arrIsArray)
_ArrayDisplay($arrLessElements)
_ArrayDisplay($arrBlankElements)

; To detect if the array has less elements than normal, use UBound()
If UBound($arrIsArray) < UBound($arrLessElements) Then
    ConsoleWrite("$arrLessElements has elements than $arrIsArray." & @CRLF)
EndIf

; To detect if the array has a blank element
For $i = 0 To UBound($arrBlankElements) - 1 Step 1
    If $arrBlankElements[$i] = "" Then ConsoleWrite("Detected a blank element at the index '" & $i & "'." & @CRLF)
Next

; As you can see from here, only $varNotAnArray, which is not an array, displays "False" as a result of IsArray() function
ConsoleWrite("Is $arrIsArray an array? " & (IsArray($arrIsArray) ? "True" : "False") & @CRLF & _             ; True
             "Is $arrLessElements an array? " & (IsArray($arrLessElements) ? "True" : "False") & @CRLF & _   ; True
             "Is $arrBlankElements an array? " & (IsArray($arrBlankElements) ? "True" : "False") & @CRLF & _ ; True
             "Is $varNotAnArray an array? " & (IsArray($varNotAnArray) ? "True" : "False") & @CRLF)          ; False

 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

@FrancescoDiMuro 

Résultat de recherche d'images pour "smiley love" 

 

Loved that one. 

Edited by caramen

My video tutorials : HERE ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

StringSplit always returns an array, even if you use a blank line, see example below, personally I would just use the line count like in the example below or use Ubound as FrancescoDiMuro used.

#include <Array.au3>
Local $aArray, $aString[] = ["One,Two,Three", "", "Four,Five,Six,Seven,Eight","Nine,Ten"]
For $i = 0 To UBound($aString) - 1
    $aArray = StringSplit($aString[$i], ",")
    ;~ Check string has been split into three or more items
    If $aArray[0] >= 3 Then
        MsgBox(4096, "Success", "String has successfully been split into " & $aArray[0] & " line items.")
    Else
        MsgBox(16, "Failure", "String has been split into " & $aArray[0] & " line items.")
    EndIf
    _ArrayDisplay($aArray)
Next

 

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

  • Similar Content

    • By sksbir
      Hi
      Trying this from autoit v3.3.14.5 and SCITE 3.19.102.1901.0 :
      help file , page "Language Reference - Variables" , sample autoit script about maps:

      Maps must be declared before use by defining their scope using the 'Global/Local/Static' keywords. Local $mControls[]
      So is my test script : only with this local declation.
      -check syntax is OK
      - running script : 

      test.au3" (13) : ==> Variable subscript badly formatted.: Local $mControls[] Local $mControls[^ ERROR ->14:51:49 AutoIt3.exe ended.rc:1
      any clue ?
    • By Blitzkid
      Hello, i want to search several directories for files with the largest numbers behind them (Like "video123") . They dont have a datatype. But there are also files with longer names and datatypes in these folders (Like "video778.mp4"). Is it possible to filter the _FileListToArray Syntax from
      to smth. like
       
      Here is my Code
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <array.au3> #include <File.au3> $filedir = @ScriptDir & "\" _checkfile() Func _checkfile() ConsoleWrite("______________________" & @CRLF) Local $arr[3] = ["music", "picture", "video"] For $i = 0 To UBound($arr) - 1 Local $arrayfiles = _FileListToArray($filedir & $arr[$i], $arr[$i] & "*", 1) If @error = 1 Then ConsoleWrite($arr[$i] & "Error 1") EndIf If @error = 4 Then ConsoleWrite($arr[$i] & "Error 2") ;Exit EndIf $arrayfilter = _ArrayMax($arrayfiles, 0, 1) Global $stringfiles = StringReplace($arrayfilter, $arr[$i], "") ConsoleWrite($arrayfilter & @CRLF) Next EndFunc ;==>_checkfile  
    • By jmp
      Script running good but error in line 7.
      When i run this script :
      #include <IE.au3> #include <Array.au3> $oIE = _IEAttach ("Shop") $oTable = _IETableGetCollection ($oIE, 1) $aTableData = _IETableWriteToArray ($oTable) For $inumber = 1 To UBound($aTableData) -1 $table = $aTableData[4][$inumber] MsgBox(0, "", $table) Next I got Error: array variable has incorrect number of subscripts or subscript dimension range exceeded
    • By nacerbaaziz
      Good morning guys, i hope that you're all well.
      guys, i have a problem and i hope that you can help me
      i've created an 3d array
      the array Contain a Categories info
      as folow
      $array[n][0][0] = Categorie name
      $array[n][0][1] = Categorie file path
      $array[n][0][2] = Categorie contents number
      $array[n][m][0] = link name
      $array[n][m][1] = link url
      $array[n][m][2] = link section name
      in my tool i want to add an option to delete a Category
      as you know the Categorie mean that must delete a region from the array
      when i tried to use _arrayDelete
      with the 2d array it work well
      but here i couldn't find any way to do that, can any one help me please?
      thanks in advance.
    • By nacerbaaziz
      hello sirs, please help me
      i tried to create a function that read a folder files to 3d array
      e.g
      $array[n][0][0] = ctName
      $array[n][0][1] = ctFilePath
      $array[n][0][2] = crtsections number
      $array[n][m][0] = KeyName
      $array[n][m][1] = KeyVal
      $array[n][m][2] = keySectionName
       
      that the array
      when i put one file into the folder all things work fine
      but when i put more than one file
      the last file worked fine but the others only the first key is showing
      please can you help me to correct this problem
      here is the example with the folder
      please accept my greetings
      and thanks in advance
       
      array3d.zip
×
×
  • Create New...