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

    • vati1011
      By vati1011
      Hey, guys.
      Currently, I'm using Opencv UDF and i'm  encountering issues with DLLOPEN(). it is failing and returning -1
      But, I cannot figure out why it is failing.
      I need help from people.
      I'm running windows 10 32bit.
      This's my code.
      #RequireAdmin $fileopen = DllOpen("opencv_core2413.dll") MsgBox(0,"Result",$fileopen) Example.au3opencv_core2413.dll
    • xiantez
      By xiantez
      This script used to work on an older version of AutoIT. Currently I am running AutoIT v3.3.14.5 and it's failing.
      Func PublicIP() ;Post public facing IP address Local $url = 'https://www.google.com/search?client=opera&q=what+is+my+ip&sourceid=opera&ie=UTF-8&oe=UTF-8' Local $getIPaddress = BinaryToString(InetRead($url)) Local $sStart = 'clamp:2">' Local $sEnd = '</div>' Local $ipaddress = _StringBetween($getIPaddress, $sStart, $sEnd For $i In $ipaddress MsgBox(0, 'External IP', "Your public IP address is " & $i) Next EndFunc ;==>PublicIP The console output shows:
      "C:\Users\user\Documents\AutoIT\Scripts\WSI Tools.au3" (197) : ==> Variable must be of type "Object".: For $i In $ipaddress For $i In $ipaddress^ ERROR ->14:12:16 AutoIt3.exe ended.rc:1 +>14:12:16 AutoIt3Wrapper Finished. >Exit code: 1 Time: 9.811
    • Xandy
      By Xandy
      I'm listing the error message strings, and roughly matching the error code
      Is this how to do error reporting?  Is there a more simple way I don't know about?
      I realise I might not need to trap @error inside $error.  I like it this way b/c I have no fear of overwriting @error now; I don't even use it.
      Func Send_Connect($address, $port)     $socket = TCPConnect($address, $port)     $error = @error     If $error <> 0 Then         Local $aError[4] = ["-2 not connected.", _                             "1 IPAddr is incorrect.", _                             "2 port is incorrect."]         $aError[3] = "10060 Connection timed out."         If $error < 0 Then $error = 0         If $error > 2 Then $error = 3         MsgBox(0, "TCP Connect Error", $aError[$error], 0)         $socket = 0     EndIf     Return $socket EndFunc
    • 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
×