Jump to content
AnonymousX

[Solved] Reassigning a 2D array each loop - Error

Recommended Posts

AnonymousX
Posted (edited)

Hello,

I'm trying to make a program that can look at a folder directory, find all the CSV files, and then add the data from CSV's to an array. 

The problem I seem to be running into is on the 2nd iteration (2nd file) when the script will not create an array. Could someone please help? Thanks in advance

 

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

Global $MasterArray


RefineData()


Func RefineData()


    Local $i, $filenum, $file, $csvArray, $sFilePath = @ScriptDir

    $fileList = _FileListToArrayRec($sFilePath, "*.csv", 1) ;Create and array of all .csv files within folder

    ;=====Loop through the .csv files within the folder======

    For $filenum = 1 To UBound($fileList) - 1 Step 1

        $file = $fileList[$filenum]
        $sFilePath = $sFilePath & "\" & $file


        ;=====Create array based on csv file=====

        _FileReadToArray($sFilePath, $csvArray, $FRTA_NOCOUNT, ",")


        _ArrayDisplay($csvArray,"File: " & $filenum)


        If $filenum = 1 Then

            $MasterArray = $csvArray
            _ArrayDisplay($MasterArray, "Master")

        Else
            $MasterArray = _ArrayColInsert($MasterArray, UBound($MasterArray)) ;want column added at end

            For $i = 0 To UBound($MasterArray)-1 Step 1

                $MasterArray[$i][UBound($MasterArray) - 1] = $csvArray[$i][4]
            Next
            _ArrayDisplay($MasterArray, "Master")
        EndIf


    Next




EndFunc   ;==>RefineData

 

Edited by AnonymousX

Share this post


Link to post
Share on other sites
kaisies

See what sFilePath is after this line each iteration of the loop. That's your problem 

$sFilePath = $sFilePath & "\" & $file
  • Like 1

Share this post


Link to post
Share on other sites
AnonymousX
On 3/27/2018 at 9:22 PM, kaisies said:

See what sFilePath is after this line each iteration of the loop. That's your problem 


$sFilePath = $sFilePath & "\" & $file

I didn't get why you thought this was the problem at first, and even in testing I just kept checking that the file name was correct. 

 

After way to long, finally realized that I can't use this line of code I need to keep the source file path constant.

 

This is the solution..... 

$FilePath = $sFilePath & "\" & $file

 

Appreciate the help, I'd recommend being less vague next time though, for dummy's like me that need things spelled out.

 

Thanks!

Share this post


Link to post
Share on other sites
Subz

Does this actually work?  You seem to overwrite your $MasterArray each time you loop through the file list.  Also you need to identify which dimension to change currently you're using 1d rather than 2d when expanding the columns, so it should be something like:

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

Global $aMasterArray

RefineData()

Func RefineData()
    Local $aCsvFile, $sFilePath = @ScriptDir
    Local $aFileList = _FileListToArrayRec($sFilePath, "*.csv", $FLTA_FILES, $FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) ;Create and array of all .csv files within folder
    ;=====Loop through the .csv files within the folder======
    For $i = 1 To $aFileList[0]
        ;=====Create array based on csv file=====
        _FileReadToArray($aFileList[$i], $aCsvFile, $FRTA_NOCOUNT, ",")
        If $i = 1 Then
            $aMasterArray = $aCsvFile
            _ArrayDisplay($aMasterArray, "Master")
        Else
            _ArrayColInsert($aMasterArray, UBound($aMasterArray, 2)) ;want column added at end
            For $j = 0 To UBound($aMasterArray) - 1
                $aMasterArray[$j][UBound($aMasterArray, 2) - 1] = $aCsvFile[$j][4]
            Next
            _ArrayDisplay($aMasterArray, "Master")
        EndIf
    Next
EndFunc   ;==>RefineData

 

  • Thanks 1

Share this post


Link to post
Share on other sites
AnonymousX
On 4/3/2018 at 8:44 PM, Subz said:

Does this actually work?  You seem to overwrite your $MasterArray each time you loop through the file list.  Also you need to identify which dimension to change currently you're using 1d rather than 2d when expanding the columns, so it should be something like:

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

Global $aMasterArray

RefineData()

Func RefineData()
    Local $aCsvFile, $sFilePath = @ScriptDir
    Local $aFileList = _FileListToArrayRec($sFilePath, "*.csv", $FLTA_FILES, $FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) ;Create and array of all .csv files within folder
    ;=====Loop through the .csv files within the folder======
    For $i = 1 To $aFileList[0]
        ;=====Create array based on csv file=====
        _FileReadToArray($aFileList[$i], $aCsvFile, $FRTA_NOCOUNT, ",")
        If $i = 1 Then
            $aMasterArray = $aCsvFile
            _ArrayDisplay($aMasterArray, "Master")
        Else
            _ArrayColInsert($aMasterArray, UBound($aMasterArray, 2)) ;want column added at end
            For $j = 0 To UBound($aMasterArray) - 1
                $aMasterArray[$j][UBound($aMasterArray, 2) - 1] = $aCsvFile[$j][4]
            Next
            _ArrayDisplay($aMasterArray, "Master")
        EndIf
    Next
EndFunc   ;==>RefineData

 

This might have been an over site in my simplified test script provided, but the root of the issue I was experiencing was that I wasn't using a constant for my directory path, as mentioned above. 

 

Appreciate the help as always man! 

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

    • Spartan117
      By Spartan117
      Hi everyone,
      I am wondering if is there any autoit function that make the program ignore errors and move on and resume correct lines?
      Thank you
    • nacerbaaziz
      By nacerbaaziz
      Hi dear
      I have a question please
      Can we customize error messages in autoit script?
      I mean internal error messages
      for example
      AutoIt Error
      Line 4  (File "D:\my projects\NVDA Manager\New AutoIt v3
      Script.au3"):
      $script[1] = 1
      $script^ ERROR
      Error: Subscript used on non-accessible variable.
      For example, I want to customize this message
      Is this possible?
      am waiting for your answers
    • ur
      By ur
      In the autoit file, if we add multiple libraries (both internal and custom created one)
       
      When we compile the code and run the exe, some time we are getting runtime errors.
      But the line number of error is not showing the exact line number, might be it is including the line number count of libraries also.
      Can you please suggest how to get the exact error line number.
      So, it will be useful for debugging.
    • Rskm
      By Rskm
      i have seen many pointing out issues related to this, i tried reading through them - still unable to solve mine.. i have a script which reads certain lines based on a search item, this is an iterative process - the ldflst_file gets updated for each iteration and the script finds the line number which has the search item. this works perfectly for few iterations, but stops in some cases abruptly with the error = 
      Subscript used on non-accessible variable $aRead^ ERROR I have verified that the search item is present in the file. what could be the reason for this code to stop in certain cases. thanks for help
      my code below (partial).. full code is in attachment
       
      local $aRead=FileReadToArray($ldflst_file)
      global $ldflst_var1=" JOINT  LOAD       FORCE(X)        FORCE(Y)        FORCE(Z)       MOMENT(X)       MOMENT(Y)       MOMENT(Z)"
       
      for $i= 0 to $ldflst_totlines    ; ldflst_totlines is total number of  lines in the file 'ldflst_file'

          if $aRead[$i]=$ldflst_var1 Then
              $ldflst_LFound=$i
              ExitLoop
              else
              EndIf
      Next
       
       
      nomo_new1.au3
    • Kevin Finnegan
      By Kevin Finnegan
      Hi all,
      Long time lurker and now forum poster! I'm writing a relatively simple backup script for my firm that automates the copy, compression and organization of Leaver's data on one of our secured NAS systems. I personally found the best method to do this so far was to use 7zG.exe (GUI version of 7Zip which can use command-line too) and it functions quite well!
      I would like to retrieve more info on whether any warnings or errors happen in 7Zip during the backup, but I can't quite get my head around the syntax and switches for reading out, it seems any adjustment I make to the RunWait call's string seems to break the backup or give unexpected repercussions! Hopefully its something silly I'm doing as I don't code very often.
      Here is the working version:
      ; Compress the directories one by one in the zip using the listfile.... Local $iPID = RunWait(@ScriptDir & "\bin\7zG.exe a -mx" & $compressionQuality & " -v" & $compressSplitFileSize & " -wc:\temp " _ & $backupToLocation & "\" & $userDirectory & ".7z @bin\listfile.txt -x@bin\excludefile.txt", "", @SW_SHOWDEFAULT, $STDOUT_CHILD) Ultimately I would love to switch entirely to 7za.exe (standalone) so that I can read the progress percentage, current file being uploaded and any warnings or errors could be processed and output to the AutoIT script's GUI I've created rather than jumping in and out of two applications per se.
       
×