Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Dynamic naming variables in a loop


  • Please log in to reply
20 replies to this topic

#1 wvzuilen

wvzuilen

    Seeker

  • Active Members
  • 7 posts

Posted 01 June 2012 - 11:16 AM

Hi all,

I got a question, but first I would like to thank all the posters of this forum. I've been coding AutoIT for a while now and I've used this forum a lot ! Thanks !
But now's I've got a question of wich I haven't find the answer so far.

I've got a folder with 50 CSV files and I would like to read those files to arrays. I would like to give the array the same name as the filename.
So filename1.csv to $filename1, filename2.csv to $filename2 etc.

My code so far:
#include <CSV.au3> #include <File.au3> #include <Array.au3> Local $aCSV = _FileListToArray(@ScriptDir & "\CSV", "*", 1) For $i = 1 To $aCSV[0] $????? = _ParseCSV(@ScriptDir & "\CSV\" & $aCSV[$i]) Next


But how the declare / name the array within the loop?
"$" & StringTrimRight($aCSV[$i],4) doesn't work (of course).







#2 water

water

    ?

  • MVPs
  • 15,706 posts

Posted 01 June 2012 - 11:20 AM

Why do you want to have 50 arrays? Can't you just read a file to an array, process it and then read the next file to the same array, process it and so on ...
UDFs:
Active Directory (NEW 2014-07-21 - Version 1.4.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2013-01-21 - Version 0.3.1.1) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

#3 wvzuilen

wvzuilen

    Seeker

  • Active Members
  • 7 posts

Posted 01 June 2012 - 11:21 AM

Why do you want to have 50 arrays? Can't you just read a file to an array, process it and then read the next file to the same array, process it and so on ...


Nope, I need all the data to calculate a insurance premium.

#4 water

water

    ?

  • MVPs
  • 15,706 posts

Posted 01 June 2012 - 11:26 AM

Use function "Assign". Example:
#include <array.au3> Global $array[3] = [1,2,3] Global $array1 Assign("array1", $array) _ArrayDisplay($array1)

UDFs:
Active Directory (NEW 2014-07-21 - Version 1.4.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2013-01-21 - Version 0.3.1.1) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

#5 czardas

czardas

  • MVPs
  • 7,120 posts

Posted 01 June 2012 - 11:28 AM

Here's a good parser for csv. I also created a csv to array function. Here's the one I wrote: although I may have overlooked the case where a csv file does not contain the delimiter. I will check that now.

Edited by czardas, 01 June 2012 - 12:12 PM.


#6 water

water

    ?

  • MVPs
  • 15,706 posts

Posted 01 June 2012 - 11:30 AM

Hi czardas,
I think the OP got the CSV reading just right. What he needs is a way to dynamically name and create the receiving arrays.
UDFs:
Active Directory (NEW 2014-07-21 - Version 1.4.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2013-01-21 - Version 0.3.1.1) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

#7 wvzuilen

wvzuilen

    Seeker

  • Active Members
  • 7 posts

Posted 01 June 2012 - 11:49 AM

Use function "Assign". Example:

#include <array.au3> Global $array[3] = [1,2,3] Global $array1 Assign("array1", $array) _ArrayDisplay($array1)

I tried this, but I cant get it to work:

#include <CSV.au3> #include <File.au3> #include <Array.au3> Local $aCSV = _FileListToArray(@ScriptDir & "CSV", "*", 1) For $i = 1 To $aCSV[0] $filename = $aCSV[$i] $aTemp = _ParseCSV(@ScriptDir & "CSV" & $filename) ;parse array to temp variable $filename = StringTrimRight($filename,4) ; remove extension Assign( $filename , $aTemp) Next _ArrayDisplay($aCSV)


#8 czardas

czardas

  • MVPs
  • 7,120 posts

Posted 01 June 2012 - 11:56 AM

Hi czardas,
I think the OP got the CSV reading just right. What he needs is a way to dynamically name and create the receiving arrays.


If it is working then fine. I posted some alternatives. I have tested them all. It turns out that I had not overlooked anything as I had previously thought.

Edit
As to the original question. How big are the files and could you use a 3D array?

Edited by czardas, 01 June 2012 - 12:06 PM.


#9 water

water

    ?

  • MVPs
  • 15,706 posts

Posted 01 June 2012 - 12:07 PM

#include <File.au3> #include <Array.au3> Local $aTemp[2] = [1, "Test"], $Test1, $Test2 Local $aCSV[3] = [2, "test1.csv", "test2.csv"] For $i = 1 To $aCSV[0]     $filename = $aCSV[$i]     $filename = StringTrimRight($filename, 4) ; remove extension     Assign($filename, $aTemp) ; Copy the array to the target array     MsgBox(16, "Message", "Is $" & $filename & " an Array: " & IsArray(Eval($filename))) ; check if target is an array Next _ArrayDisplay($test1) ; Display first target array

You can't display one of the dynamically generated arrays because _ArrayDisplay uses ByRef.
This example "knows" that the resulting arrays are named test1 and test2.
The script tests the targets for being arrays and displays the first target array.
  • czardas likes this
UDFs:
Active Directory (NEW 2014-07-21 - Version 1.4.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2013-01-21 - Version 0.3.1.1) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

#10 wvzuilen

wvzuilen

    Seeker

  • Active Members
  • 7 posts

Posted 01 June 2012 - 12:28 PM

#include <File.au3> #include <Array.au3> Local $aTemp[2] = [1, "Test"], $Test1, $Test2 Local $aCSV[3] = [2, "test1.csv", "test2.csv"] For $i = 1 To $aCSV[0]     $filename = $aCSV[$i]     $filename = StringTrimRight($filename, 4) ; remove extension     Assign($filename, $aTemp) ; Copy the array to the target array     MsgBox(16, "Message", "Is $" & $filename & " an Array: " & IsArray(Eval($filename))) ; check if target is an array Next _ArrayDisplay($test1) ; Display first target array

You can't display one of the dynamically generated arrays because _ArrayDisplay uses ByRef.
This example "knows" that the resulting arrays are named test1 and test2.
The script tests the targets for being arrays and displays the first target array.

Thanks ! With this example I got it working !

AutoIt         
#include <CSV.au3> #include <File.au3> #include <Array.au3> Local $Cilinder_cat Local $Clausule_reg Local $Commercieel1 Local $CP_kanaal Local $CP_leeft Local $CP_Loy_Pntn Local $CP_noclaim Local $Dekking_OO Local $Dichtheid Local $Eigen_Risico_code Local $Eigen_risico Local $Heading Local $Inhoudsopgave Local $KM Local $Land_code Local $Leeftijd_Cat Local $Merk Local $Merk_clause Local $No_Claim Local $OAR Local $PC_Dichtheid Local $PC_Regio Local $PF_Eigen_Risico Local $Provincie Local $Provisie Local $Provisie_MAX Local $Regio Local $RF_Bouwjaar Local $RF_Buitenl Local $RF_Catalogus Local $RF_Cilinder Local $RF_Dichtheid Local $RF_Eig_Risk Local $RF_KM Local $RF_Leeftijd Local $RF_Merk Local $RF_Provincie Local $RF_Regio Local $RF_Type_motor Local $Risk Local $Type Local $Variabelen Local $VB_Risk Local $Voertuigsrt Local $aCSV = _FileListToArray(@ScriptDir & "CSV", "*", 1) For $i = 1 To $aCSV[0]     $filename = $aCSV[$i] Local $aTemp = _ParseCSV(@ScriptDir & "CSV" & $filename) ;parse array to temp variable     $filename = StringTrimRight($filename, 4) ; remove extension     Assign($filename, $aTemp) ; Copy the array to the target array Next _ArrayDisplay($Cilinder_cat) ; Display first target array


#11 water

water

    ?

  • MVPs
  • 15,706 posts

Posted 01 June 2012 - 12:30 PM

Glad you got it working ;)
UDFs:
Active Directory (NEW 2014-07-21 - Version 1.4.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2013-01-21 - Version 0.3.1.1) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

#12 Mechaflash

Mechaflash

    Sons of Anarchy

  • Active Members
  • PipPipPipPipPipPip
  • 1,377 posts

Posted 01 June 2012 - 01:17 PM

Thanks ! With this example I got it working !

Spoiler


I'm confused... I see _ArrayDisplay($Cilinder_cat) at the end of the script... but I see nothing being put into it???

Edited by mechaflash213, 01 June 2012 - 01:18 PM.

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

#13 water

water

    ?

  • MVPs
  • 15,706 posts

Posted 01 June 2012 - 01:19 PM

The name of the Array is dynamically created from the filenames read by _FileListToArray and then filled by _ParseCSV.
UDFs:
Active Directory (NEW 2014-07-21 - Version 1.4.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2013-01-21 - Version 0.3.1.1) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

#14 czardas

czardas

  • MVPs
  • 7,120 posts

Posted 01 June 2012 - 06:10 PM

water, if the function _ParseCSV is the one I'm thinking of, then it contains a bug. One that is easy to fix though. ;)

Edited by czardas, 01 June 2012 - 06:15 PM.


#15 water

water

    ?

  • MVPs
  • 15,706 posts

Posted 01 June 2012 - 06:15 PM

czardas,
the one I have in mind is this one written by ProgAndy which I also use in my OutlookEX UDF.
If there is a bug I would surely like to hear about it.
UDFs:
Active Directory (NEW 2014-07-21 - Version 1.4.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2014-07-27 - Version 1.0.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2013-01-21 - Version 0.3.1.1) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

#16 czardas

czardas

  • MVPs
  • 7,120 posts

Posted 01 June 2012 - 06:24 PM

I think the UDF is in many ways superior having Unicode support, and that ProgAndy is an awesome programmer. The following csv data taken from http://en.wikipedia.org/wiki/Comma-separated_values#Example returns 4 rows when it should return 5 rows. It also seems to return an extra column, I'm not sure if that is intentional.

Year,Make,Model,Description,Price 1997,Ford,E350,"ac, abs, moon",3000.00 1999,Chevy,"Venture ""Extended Edition""","",4900.00 1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00 1996,Jeep,Grand Cherokee,"MUST SELL! air, moon roof, loaded",4799.00


ProgAndy sent me an alternative function by PM earlier.:

AutoIt         
Func _ParseCSV($sFile, $sDelimiters=',;', $sQuote='"', $iFormat=0)     Local Static $aEncoding[6] = [0, 0, 32, 64, 128, 256]     If $iFormat < -1 Or $iFormat > 6 Then         Return SetError(3,0,0)     ElseIf $iFormat > -1 Then         Local $hFile = FileOpen($sFile, $aEncoding[$iFormat]), $sLine, $aTemp, $aCSV[1], $iReserved, $iCount         If @error Then Return SetError(1,@error,0)         $sFile = FileRead($hFile)         FileClose($hFile)     EndIf     If $sDelimiters = "" Or IsKeyword($sDelimiters) Then $sDelimiters = ',;'     If $sQuote = "" Or IsKeyword($sQuote) Then $sQuote = '"'     $sQuote = StringLeft($sQuote, 1)     Local $srDelimiters = StringRegExpReplace($sDelimiters, '[^-[]]', '0')     Local $srQuote = StringRegExpReplace($sQuote, '[^-[]]', '0')     Local $sPattern = StringReplace(StringReplace('(?m)(?:^|[,])h*(["](?:[^"]|["]{2})*["]|[^,rn]*)(v+)?',',', $srDelimiters, 0, 1),'"', $srQuote, 0, 1)     Local $aREgex = StringRegExp($sFile, $sPattern, 3)     If @error Then Return SetError(2,@error,0)     $sFile = '' ; save memory     Local $iBound = UBound($aREgex), $iIndex=0, $iSubBound = 1, $iSub = 0     Local $aResult[$iBound][$iSubBound], $fAdded = False     For $i = 0 To $iBound-1         Select             Case StringLen($aREgex[$i])<3 And StringInStr(@CRLF, $aREgex[$i])                 $fAdded = False                 $iIndex += 1                 $iSub = 0                 ContinueLoop             Case StringLeft(StringStripWS($aREgex[$i], 1),1)=$sQuote                 $fAdded = True                 $aREgex[$i] = StringStripWS($aREgex[$i], 3)                 $aResult[$iIndex][$iSub] = StringReplace(StringMid($aREgex[$i], 2, StringLen($aREgex[$i])-2), $sQuote&$sQuote, $sQuote, 0, 1)             Case Else                 $fAdded = True                 $aResult[$iIndex][$iSub] = $aREgex[$i]         EndSelect         $aREgex[$i]=0 ; save memory         $iSub += 1         If $iSub = $iSubBound Then             $iSubBound += 1             ReDim $aResult[$iBound][$iSubBound]         EndIf     Next     If $iIndex = 0 Then $iIndex=1     ReDim $aResult[$iIndex+$fAdded][$iSubBound]     Return $aResult EndFunc

Edited by czardas, 01 June 2012 - 06:29 PM.


#17 AdmiralClaws

AdmiralClaws

    Where is the fish?

  • Active Members
  • PipPipPipPipPipPip
  • 4,697 posts

Posted 01 June 2012 - 06:35 PM

Assign() is ugly. Use an array instead. It will be simpler too.

#18 Newb

Newb

    Polymath

  • Active Members
  • PipPipPipPip
  • 203 posts

Posted 01 June 2012 - 06:45 PM

Assign rocks.... never knew about it and have been searching for it in a while

It's a lately implemented function or it exists since lot of time?

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.


#19 wvzuilen

wvzuilen

    Seeker

  • Active Members
  • 7 posts

Posted 01 June 2012 - 06:50 PM

I think the UDF is in many ways superior having Unicode support, and that ProgAndy is an awesome programmer. The following csv data taken from http://en.wikipedia.org/wiki/Comma-separated_values#Example returns 4 rows when it should return 5 rows. It also seems to return an extra column, I'm not sure if that is intentional.


I noticed that, I edited one of my CSV files and removed a trailing, useless linefeed. But the array created by ProgAndy's _ParseCSV then missed a row. So I added the linefeed again ;)

#20 czardas

czardas

  • MVPs
  • 7,120 posts

Posted 01 June 2012 - 06:56 PM

Yeah probably best to leave the final Line Feed alone. I didn't realize it earlier, but that was the problem. ;)

Edited by czardas, 01 June 2012 - 07:04 PM.



Back to General Help and Support


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users