maloysius Posted April 9, 2020 Share Posted April 9, 2020 Hello everyone, I am trying to create a program that writes custom script to a text file, and then a third party program runs it. There are 2 arrays that contain data. I need the second array to loop entirely through printing all of it's information for each slot in the first array, like the following: Array1 Info [1] Array2 Info [1] Array2 Info [2] Array2 Info [3] Array1 Info [2] Array2 Info [1] Array2 Info [2] Array2 Info [3] etc... I have been messing around with code, this is what I'm using right now (cut out just the useful parts): #include <file.au3> #include <WindowsConstants.au3> #include <FileConstants.au3> #include <MsgBoxConstants.au3> $sFileName = "C:\Parameters.txt" ;The file that I am writing all this script to $Data = _GUICtrlListBox_GetSelItemsText($List1) ;Gets data from a list that I have, which works fine $Upload = GUICtrlRead($UploadList) ;This is just an edit box that is being read raw $UploadArray = _CSVSplit($Upload,@TAB) ;Uploading the $Upload information into an array variable using a slightly altered version of the CSVSplit UDF _ArrayDisplay($UploadArray) ;This displays the $UploadArray variable, and it displays CORRECTLY, so I know this works this far For $iI = 1 To $Data[0] $sItems = "" $sItems &= @CRLF & $Data[$iI] FileWrite($hFilehandle, @CRLF & 'cd "/FTP/Files/' & StringStripWS($sItems,$STR_STRIPLEADING) & '/"') ;This part works great, it loops through fine For $iT = 1 To $UploadArray[0] $ArrayItems = "" $ArrayItems &= @CRLF & $UploadArray[$iT] FileWrite($hFilehandle, @CRLF & "put " & $ArrayItems) ;This part does NOT work great...it just puts nothing every time, regardless of info in array Next Next I made the code for the loop within the loop exactly the same as the first part of the loop, which works well and loops through everything in the array. But it just doesn't do anything when it gets to the loop within the loop, even though I verified that the second array is getting filled correctly (at least to my knowledge!). Here is the lightly modified code for the _CSVSplit function (I literally just changed the $sDelim to @CRLF). Although I don't think this is the problem since it seems to be populating correctly, I am including just for the sake of completeness and in case anyone wanted to reference this part. expandcollapse popupFunc _CSVSplit($string, $sDelim = @CRLF) ; Parses csv string input and returns a one or two dimensional array If Not IsString($string) Or $string = "" Then Return SetError(1, 0, 0) ; Invalid string If Not IsString($sDelim) Or $sDelim = "" Then Return SetError(2, 0, 0) ; Invalid string $string = StringRegExpReplace($string, "[\r\n]+\z", "") ; [Line Added] Remove training breaks Local $iOverride = 63743, $asDelim[3] ; $asDelim => replacements for comma, new line and double quote For $i = 0 To 2 $asDelim[$i] = __GetSubstitute($string, $iOverride) ; Choose a suitable substitution character If @error Then Return SetError(3, 0, 0) ; String contains too many unsuitable characters Next $iOverride = 0 Local $aArray = StringRegExp($string, '\A[^"]+|("+[^"]+)|"+\z', 3) ; Split string using double quotes delim - largest match $string = "" Local $iBound = UBound($aArray) For $i = 0 To $iBound -1 $iOverride += StringInStr($aArray[$i], '"', 0, -1) ; Increment by the number of adjacent double quotes per element If Mod ($iOverride +2, 2) = 0 Then ; Acts as an on/off switch $aArray[$i] = StringReplace($aArray[$i], $sDelim, $asDelim[0]) ; Replace comma delimeters $aArray[$i] = StringRegExpReplace($aArray[$i], "(\r\n)|[\r\n]", $asDelim[1]) ; Replace new line delimeters EndIf $aArray[$i] = StringReplace($aArray[$i], '""', $asDelim[2]) ; Replace double quote pairs $aArray[$i] = StringReplace($aArray[$i], '"', '') ; Delete enclosing double quotes - not paired $aArray[$i] = StringReplace($aArray[$i], $asDelim[2], '"') ; Reintroduce double quote pairs as single characters $string &= $aArray[$i] ; Rebuild the string, which includes two different delimiters Next $iOverride = 0 $aArray = StringSplit($string, $asDelim[1], 2) ; Split to get rows $iBound = UBound($aArray) Local $aCSV[$iBound][2], $aTemp For $i = 0 To $iBound -1 $aTemp = StringSplit($aArray[$i], $asDelim[0]) ; Split to get row items If Not @error Then If $aTemp[0] > $iOverride Then $iOverride = $aTemp[0] ReDim $aCSV[$iBound][$iOverride] ; Add columns to accomodate more items EndIf EndIf For $j = 1 To $aTemp[0] If StringLen($aTemp[$j]) Then If Not StringRegExp($aTemp[$j], '[^"]') Then ; Field only contains double quotes $aTemp[$j] = StringTrimLeft($aTemp[$j], 1) ; Delete enclosing double quote single char EndIf $aCSV[$i][$j -1] = $aTemp[$j] ; Populate each row EndIf Next Next If $iOverride > 1 Then Return $aCSV ; Multiple Columns Else For $i = 0 To $iBound -1 If StringLen($aArray[$i]) And (Not StringRegExp($aArray[$i], '[^"]')) Then ; Only contains double quotes $aArray[$i] = StringTrimLeft($aArray[$i], 1) ; Delete enclosing double quote single char EndIf Next Return $aArray ; Single column EndIf EndFunc ;==> _CSVSplit Func __GetSubstitute($string, ByRef $iCountdown) If $iCountdown < 57344 Then Return SetError(1, 0, "") ; Out of options Local $sTestChar For $i = $iCountdown To 57344 Step -1 $sTestChar = ChrW($i) $iCountdown -= 1 If Not StringInStr($string, $sTestChar) Then Return $sTestChar EndIf Next Return SetError(1, 0, "") ; Out of options EndFunc ;==> __GetSubstitute Does anyone have any ideas on why this seems to be happening? Is it something super easy that I've just overlooked or don't know about?? Is it even possible to nest for loops like this? Thank you in advance folks!! Link to comment Share on other sites More sharing options...
Subz Posted April 9, 2020 Share Posted April 9, 2020 Are you sure $UploadArray is a 1d array or is it a 2d array? Use _ArrayDisplay to error check. Link to comment Share on other sites More sharing options...
maloysius Posted April 9, 2020 Author Share Posted April 9, 2020 Hey Subz, Thank you for the quick reply! As I am still fairly new to this whole array thing, so I was unaware of differences such as 1d and 2d. This is a snapshot of the array display once I loaded some data into it: Is there a way I can easily tell? I presume that it's 1d because it has one column only right...? Link to comment Share on other sites More sharing options...
Nine Posted April 9, 2020 Share Posted April 9, 2020 It is at the bottom that you know if it is 1d or 2d. So you got [4], that tells you it is 1D. You could have the exact same display with a 2D array but the bottom would say something like [4][1]. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
maloysius Posted April 9, 2020 Author Share Posted April 9, 2020 Gotcha, thanks for the clarification Nine! So since it is a 1d array, any ideas why the second part of the loop doesn't seem to be populating? Link to comment Share on other sites More sharing options...
Nine Posted April 9, 2020 Share Posted April 9, 2020 It is a 0-base array not a 1-base like you seem to believe. Just use : For $iT = 0 To UBound($UploadArray) - 1 maloysius 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
maloysius Posted April 9, 2020 Author Share Posted April 9, 2020 Well I'll be darned, thank you Nine, it works like a champ now!! I appreciate the clarification and info. Have a great one everyone! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now