Jump to content
Sign in to follow this  
NiceBoy1234

Get Array Index of for loop in if statement

Recommended Posts

NiceBoy1234

How can I access a variable with its index of a loop inside my if statment.

Code:
 

For $i = 0 To UBound($aFileListFootageFolderRe_01) - 1
      ...
      Next

     If $getTimeNewFootageFolderInit_01 <> $getTimeNewFootageFolder_01 And $aFileListFootageFolderRe_01[$i] == 11) Then
      ...
     EndIf

I get following error while doing this:
 

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

Thus the error points at this

$aFileListFootageFolderRe_01[$i]

Share this post


Link to post
Share on other sites
orbs

to debug, before the "For" section, call _ArrayDisplay() to make sure the array exists and is 1-D.

also, i assume you mean the "Next" line should be after the If...EndIf, so it is inside the loop and performed over every element, right? if not, then make sure $i has a valid value after the loop is done.

Share this post


Link to post
Share on other sites
NiceBoy1234

No Next should not be after If Endif, so after the for loop I have to declare my "i" like $i = 0.
Thus what do you mean excatly with 1-D?
nevermind 1 Dimensional :)

Edited by NiceBoy1234

Share this post


Link to post
Share on other sites
NiceBoy1234

Ok I did _ArrayDisplay() and it's 1 Dimensional.

Share this post


Link to post
Share on other sites
NiceBoy1234

I gave $i a valid value after the looop but did not solve it either

Share this post


Link to post
Share on other sites
InunoTaishou

You could use _ArraySearch to get the index of the value you're looking for

#include <Array.au3>
Global const $aValues[] = ["This", "Is", "a", "test", "AutoIt", "Rulez!"]
Global const $sValueToFind1 = "AutoIt"
Global const $sValueToFind2 = "Cake"
Global const $iIndex1 = _ArraySearch($aValues, $sValueToFind1)
Global const $iIndex2 = _ArraySearch($aValues, $sValueToFind2)

If ($iIndex1 > -1) Then
    MsgBox("", "", "Found '" & $aValues[$iIndex1] & "' at index " & $iIndex1)
Else
    MsgBox("", "", "Could not find the value '" & $sValueToFind1 & "' for index $iIndex1")
EndIf

If ($iIndex2 > -1) Then
    MsgBox("", "", "Found '" & $aValues[$iIndex2] & "' at index " & $iIndex2)
Else
    MsgBox("", "", "Could not find the value '" & $sValueToFind2 & "' for index $iIndex2")
EndIf

 

Edited by InunoTaishou

Share this post


Link to post
Share on other sites
orbs

please attach a screenshot of _ArrayDisplay (or a console output you can generate with this) and the value of $i right before the offending "If".

Share this post


Link to post
Share on other sites
NiceBoy1234

_ArrayDisplay showed a window with this:

[0] = "xxx"

[1] = "xxx"

[2] = "xxx"

Share this post


Link to post
Share on other sites
orbs

and $i ...?

Share this post


Link to post
Share on other sites
NiceBoy1234

$i stands for [0][1][2] or not?,
it jsut showed

[0] = "xxx"

[1] = "xxx"

[2] = "xxx"

Share this post


Link to post
Share on other sites
orbs

add this line before the offending "If" line:

ConsoleWrite('$i = ' & $i & @CRLF)

$i should be an integer. 0, 1 or 2 are valid values in this case.

Share this post


Link to post
Share on other sites
NiceBoy1234

yep I get 0, 1, 2

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  

  • Similar Content

    • Skeletor
      By Skeletor
      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  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) 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] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • Skeletor
      By Skeletor
      Hi Guys,
      Is it possible to get a variable on your For..Next loop? 
       
      Local $Lines1 = _FileCountLines(C:\temp\test.txt) Local $linesToCount2 = $Lines1 + 2 $var = Number($linesToCount2) For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = $var To $count Next ;Code does stuff here Next Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.
      This code I posted above does not move to the next value.
      But the code below does... why is that?
      For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = 2 To $count Next ;Code does stuff here Next  
      Why is the For loop resetting itself?
      Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?
      If so , how would you make this variable static?

       
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
×