mike2003 Posted June 19, 2021 Share Posted June 19, 2021 I have a set of items in a file. I read line by line. Depending on the content, the data goes into arrays. How can I switch arrays conveniently? Something more like a reference to a variable from C ++. For example. Dim $a[0], $b[0] $t = FileReadLine if $t = aaaa then arr = $a if $t = bbbb then arr = $b _ArrayAdd(arr, $t) Is something like this possible? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 19, 2021 Moderators Share Posted June 19, 2021 (edited) mike2003, i would do something like this: Global $a[0], $b[0] $hFile = FileOpen($sFile) While 1 $t = FileReadLine($hFile) Switch $t Case -1 ; EOF ExitLoop Case "aaaa" _ArrayAdd($a, $t) Case "bbbb" _ArrayAdd($b, $t) EndSwitch WEnd FileClose($hFile) M23 P.S. Click here to see how to post code. Edited June 19, 2021 by Melba23 Typo Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
jchd Posted June 19, 2021 Share Posted June 19, 2021 Yes but not pretty: Local $a[0], $b[0], $arr $t = FileReadLine If $t = "aaaa" Then $arr = "a" If $t = "bbbb" Then $arr = "b" _ArrayAdd(Eval($arr), $t) This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
mike2003 Posted June 19, 2021 Author Share Posted June 19, 2021 (edited) 1 hour ago, jchd said: _ArrayAdd(Eval($arr), $t) This is very similar to what I need. But it doesn't work. error: _ArrayAdd() called with Const or expression on ByRef-param(s). Quote Eval Return the value of the variable defined by a string. Not a pointer to an array address. Edited June 19, 2021 by mike2003 Link to comment Share on other sites More sharing options...
mike2003 Posted June 19, 2021 Author Share Posted June 19, 2021 1 hour ago, Melba23 said: do something like this Not in my case. I have a more complex data structure. Marker1 data11111 data2222 data333 Marker2 new data11111 new data2222 new data333 new data44 Marker3 ... Link to comment Share on other sites More sharing options...
Subz Posted June 19, 2021 Share Posted June 19, 2021 You would need to use something like StringInStr for example: #include <Array.au3> Global $a[0], $b[0] Global $sFile = @ScriptDir & "\Example.txt" Global $hFile = FileOpen($sFile) While 1 $t = FileReadLine($hFile) If @error Then ExitLoop If StringInStr($t, "data1") Then _ArrayAdd($a, $t) ElseIf StringInStr($t, "data2") Then _ArrayAdd($b, $t) EndIf WEnd FileClose($hFile) _ArrayDisplay($a) _ArrayDisplay($b) Link to comment Share on other sites More sharing options...
mike2003 Posted June 19, 2021 Author Share Posted June 19, 2021 (edited) 18 minutes ago, Subz said: StringInStr But my data does not contain labels ('data1' & ' data2'), only "Marker" in the header. And that means all this will be lost. I just need to switch the target array. Edited June 19, 2021 by mike2003 Link to comment Share on other sites More sharing options...
Subz Posted June 19, 2021 Share Posted June 19, 2021 Your posts are little confusing, in your original post you mentioned, aaaaa or bbbbb (which Melba23 referenced) you then said it wouldn't work because the data structure was different (which I was referencing). You need to clarify what you're after or modify the examples we posted to fit your needs. Link to comment Share on other sites More sharing options...
JockoDundee Posted June 19, 2021 Share Posted June 19, 2021 #include <Array.au3> Global $garr[2], $arr, $s[0], $t $garr[0]=$s $garr[1]=$s $t=FileReadLine("file.txt") If $t="aaaa" Then $arr=0 If $t="bbbb" Then $arr=1 _ArrayAdd($garr[$arr], $t) _ArrayDisplay($garr[$arr]) Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
JockoDundee Posted June 19, 2021 Share Posted June 19, 2021 Modification, if you like your $a and $b stuff: #include <Array.au3> Global $garr[2], $arr, $s[0], $t Enum $a, $b $garr[$a]=$s $garr[$b]=$s $t=FileReadLine("file.txt") If $t="aaaa" Then $arr=$a If $t="bbbb" Then $arr=$b _ArrayAdd($garr[$arr], $t) Gianni 1 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
jchd Posted June 19, 2021 Share Posted June 19, 2021 (edited) 3 hours ago, mike2003 said: This is very similar to what I need. But it doesn't work. error: _ArrayAdd() called with Const or expression on ByRef-param(s). You're very right. I made the mistake to post quickly, seconds before leaving my home, and didn't engage brain when typing. Edited June 19, 2021 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
mikell Posted June 19, 2021 Share Posted June 19, 2021 A more convenient (and efficient) way could be to use StringRegExp to directly build the arrays instead of populate them progressively Concept : $txt = FileRead ; define requirements and build the patterns $pattern_a = ... $pattern_b = ... ; then use regex with flag $STR_REGEXPARRAYGLOBALMATCH ; to return the 2 arrays $a = StringRegExp($txt, $pattern_a, 3) $b = StringRegExp($txt, $pattern_b, 3) Link to comment Share on other sites More sharing options...
mike2003 Posted June 20, 2021 Author Share Posted June 20, 2021 (edited) Sorry, but either it doesn't work or I don't understand. Here is some primitive long code that I want to reduce and optimize. It has more lines and is very awkward. $sFileRead = FileReadLine($hFileOpen) ElseIf ($sFileRead = "mark1") Then $ArrMode = 1 ElseIf ($sFileRead = "mark2") Then $ArrMode = 2 ElseIf ($sFileRead = "mark3) Then $ArrMode = 3 EndIf Switch $ArrMode Case 1 _ArrayAdd($Arr1, $sFileRead) Case 2 _ArrayAdd($Arr2, $sFileRead) Case 3 _ArrayAdd($Arr3, $sFileRead) EndSwitch Edited June 20, 2021 by mike2003 Link to comment Share on other sites More sharing options...
Subz Posted June 20, 2021 Share Posted June 20, 2021 Maybe something like: #include <Array.au3> Global $Arr1[0], $Arr2[0], $Arr3[0] Global $sFile = @ScriptDir & "\Example.txt" Global $hFileOpen = FileOpen($sFile) While 1 $sFileRead = FileReadLine($hFileOpen) If @error Then ExitLoop If (StringStripWS($sFileRead,8) = "mark1") Then $ArrMode = 1 ElseIf (StringStripWS($sFileRead,8) = "mark2") Then $ArrMode = 2 ElseIf (StringStripWS($sFileRead,8) = "mark3") Then $ArrMode = 3 ElseIf (StringStripWS($sFileRead,8) = "") Then ContinueLoop EndIf Switch $ArrMode Case 1 _ArrayAdd($Arr1, $sFileRead) Case 2 _ArrayAdd($Arr2, $sFileRead) Case 3 _ArrayAdd($Arr3, $sFileRead) EndSwitch WEnd _ArrayDisplay($Arr1) _ArrayDisplay($Arr2) _ArrayDisplay($Arr3) Link to comment Share on other sites More sharing options...
mikell Posted June 20, 2021 Share Posted June 20, 2021 @Subz The requirements are confusing. For the fun... #include <Array.au3> $sFile = FileRead(@ScriptDir & "\Example.txt") $a1 = _getArray("Marker1") $a2 = _getArray("Marker2") $a3 = _getArray("Marker3") _ArrayDisplay($a1) _ArrayDisplay($a2) _ArrayDisplay($a3) Func _getArray($header) ; including the header Local $s = StringRegExpReplace($sFile, '(?ms).*?(' & $header & '.*?)(?=\R{2}|\z).*', "$1") ; excluding the header ;Local $s = StringRegExpReplace($sFile, '(?ms).*?' & $header & '\R(.*?)(?=\R{2}|\z).*', "$1") Return StringRegExp($s, '\N+', 3) EndFunc Subz 1 Link to comment Share on other sites More sharing options...
JockoDundee Posted June 20, 2021 Share Posted June 20, 2021 2 hours ago, mike2003 said: It has more lines and is very awkward. My solution has no Switch and no multiple ArrayAdds. It only has conditionals to evaluate the marker, which I believe are necessary. What more were you looking to abstract? Code hard, but don’t hard code... 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