Jump to content

Syntax error when using += in array index


Recommended Posts

I've written a script with a section that populates a ListView with data.  Before populating the ListView, I get all the data inserted into an array in the order I want.  Originally I was doing it as follows:

$tArr[1] = IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ReqNum",'')
$tArr[2] = IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ItemNum",'')
$tArr[3] = IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "RMANum",'')
; etc..

This makes it a little tedious if I want to add a value or reorder them so I tried this instead:

$k = 0
$tArr[$k += 1] = IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ReqNum",'')
$tArr[$k += 1] = IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ItemNum",'')
$tArr[$k += 1] = IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "RMANum",'')

Unfortunately I'm getting syntax errors where I've tried to put the incrementing $k value as the array index.  Is there a way to do what I'm trying to do?

Thanks in advance!

Jimbo

Using AutoIt v3.3.14.5 and SciTE version 4.2.0

Link to comment
Share on other sites

Arrays start at 0 + you need to expand your $tArr[0] using ReDim or alternatively you can use something like:

#include <Array.au3>
Local $i = 0, $tArr[0]
    _ArrayAdd($tArr, IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ReqNum",''))
    _ArrayAdd($tArr, IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ItemNum",''))
    _ArrayAdd($tArr, IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "RMANum",''))

 

Edited by Subz
Link to comment
Share on other sites

5 minutes ago, Subz said:

Arrays start at 0 + you need to expand your $tArr[0] using ReDim or alternatively you can use something like:

#include <Array.au3>
Local $aFileList = _FileListToArrayRec(...), $tArr[0]
For $i = 1 To $aFileList[0]
    _ArrayAdd($tArr, IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ReqNum",''))
Next

 

Thanks for the quick response.  I've already got the ReDim handled, I just didn't show that part.  I'd love to use a For loop but in this case I'm picking specific values from a large ini file for the ListView columns and don't want to show them all.  Also order is important.  That's why I'm picking each one based on the key (ReqNum, ItemNum, RMANum, etc).

7 minutes ago, FrancescoDiMuro said:

@gi_jimbo
Never heard about _ArrayAdd() function? :)

Ha! Adding items individually to an array seemed so simple to me that I didn't even think to look for a function for it.  Thanks for that!  I'm sure I can make that work.

Jimbo

Using AutoIt v3.3.14.5 and SciTE version 4.2.0

Link to comment
Share on other sites

@Subz I did and I understand where you're coming from.  I really appreciate your quick attempt to help.  Those are great suggestions for problems I'm not having.  I'm not sure you understood my original problem and my reply.  I can't use a loop as the items have to be added to the array specifically by name.

Thanks!

Jimbo

Using AutoIt v3.3.14.5 and SciTE version 4.2.0

Link to comment
Share on other sites

nm, the for/next loop was unrelated, it was just an example of looping through the file list to get the $i value, however maybe you're just using a static $i variable, the _ArrayAdd was adding the contents to the $tArr, I left the other two lines out, I assumed you had more than 3 lines, I've now updated the code above to show what I meant.

Link to comment
Share on other sites

Here's some more perspective on the code:

ReDim $tArr[_GUICtrlListView_GetColumnCount($lv_S_Requests)+1]
For $i = 1 To $aFileList[0]
    $tArr[1] = IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ReqNum",'')
    $tArr[2] = IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ItemNum",'')
    $tArr[3] = IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "RMANum",'')
    ; lots more rows
    GUICtrlCreateListViewItem(_ArrayToString($tArr,'|',1), $lv_S_Requests)
Next

Since I've already ReDim'd the array before starting to populate it, _ArrayAdd is adding the values at the end of where I ReDim'd it to.  I'm guessing I need to drop the Redim if I'm going to use _ArrayAdd.  Also I'm guessing I'll have to delete the array or redim to 0 just before the end of the for $i loop.  I'm guessing _ArrayAdd does a ReDim operation every time it gets called then.  Is this going to slow things down?

@Subz This portion of code is within an already existing For loop for $i.  You're also correct that I have a lot more than 3 lines.  It's populating each row by grabbing the specific items for each column from each record in a $aFileList array.

So really all I want to do is increment the index of $tArr for each line.  In C++ I'd use k++.  Is there an equivalent in AutoIT?

Edited by gi_jimbo
1st part of my response was for everyone.

Jimbo

Using AutoIt v3.3.14.5 and SciTE version 4.2.0

Link to comment
Share on other sites

You could just use a string then

Global $sListViewItem = ""
For $i = 1 To $aFileList[0]
    $sListViewItem = IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ReqNum",'')
    $sListViewItem &= "|" & IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "ItemNum",'')
    $sListViewItem &= "|" & IniRead($SaveLoc &'\'& $aFileList[$i], "Request Info", "RMANum",'')
    ; lots more rows
    GUICtrlCreateListViewItem($sListViewItem, $lv_S_Requests)
Next

 

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