bailey1274 Posted June 19, 2020 Posted June 19, 2020 expandcollapse popup#include <Array.au3> LocalFunction() Func LocalFunction() ; Goal is to have one function act on all of these Local $Var1 = ' Trim Me 1 ' Local $Var2 = ' Trim Me 2 ' Local $Var3 = ' Trim Me 3 ' ; Function/Params of what you want to apply to a list/set of vars Local $FuncArr[2] = [StringStripWS, 3] ; Var names next to vars. If we could byRef these somehow to avoid the For loop after the Apply then it would be great Local $VarArr[6] = [$Var1, 'Var1', $Var2, 'Var2', $Var3, 'Var3'] ; Apply our function to our list of variables Apply($FuncArr, $VarArr) ; Reassign our updated array back to our original variables Local $val For $var=0 to UBound($VarArr)-1 If Mod($var, 2) = 0 Then ; Grab the updated values $Val = $VarArr[$var] ContinueLoop EndIf Assign($VarArr[$var], $Val) ; Assign value to correct name Next MsgBox(0,'Var1', '['&$Var1&']') MsgBox(0,'Var2', '['&$Var2&']') MsgBox(0,'Var3', '['&$Var3&']') EndFunc Func Apply($aFunc, byRef $arr) For $var=0 to UBound($arr)-1 If Mod($var, 2) <> 0 Then ContinueLoop ; Avoid the variable name strings $arr[$var] = Call($aFunc[0], $arr[$var], $aFunc[1]) Next EndFunc So heres the code, it gets close to what I want although does anyone have any ideas how to avoid the For Loop after the apply function? If there is a way to create an array with references back to the variables themselves instead of just passing the values, this wouldn't be so ugly. The goal is to just be able to call one function on a large set of variables without having to reassign them individually. This does work but, imo, its pretty ugly and would only be worth it for a very large set of variables since the implementation of this pattern is at minimum like 10 lines.
jchd Posted June 19, 2020 Posted June 19, 2020 Seems to me you're making your life difficult: LocalFunction() Func LocalFunction() ; Goal is to have one function act on all of these Local $aVars = [' Trim Me 1 ', ' Trim Me 2 ', ' Trim Me 3 '] ; Function/Params of what you want to apply to a list/set of vars Local $aFunction = [StringStripWS, 3] ; Apply function to array elements For $i = 0 to UBound($aVars) - 1 $aVars[$i] = $aFunction[0]($aVars[$i], $aFunction[1]) Next _ArrayDisplay($aVars) EndFunc 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)
bailey1274 Posted June 22, 2020 Author Posted June 22, 2020 The goal is to have a callable function that can be used and updates variables, not arrays of values. I think with the limitations of autoit, it may not be possible. It could be achieved using all global variables but that's something I don't want to do. The only way to have a callable function that updates the original local variables in the function would to be using byRefs but also having an arbitrary number of params. This can't be done since optional byRefs aren't supported which is why I put the values into an array with the corresponding names next to them to use after the array has been cleaned and then assign back to original vars. I was just hoping someone had a not so intuitive way that I'm not seeing how to do it.
jchd Posted June 22, 2020 Posted June 22, 2020 Python map() doesn't directly do what you want either: you still have to convert the resulting map object(s) to something else AND initial (caller's side) arguments aren't changed either. I agree, AutoIt isn't a functional language at all. 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)
bailey1274 Posted June 22, 2020 Author Posted June 22, 2020 But python can at least unpack things in one line, The verbosenss of a rather simple task the issue I am trying to handle here.
Moderators Melba23 Posted June 22, 2020 Moderators Posted June 22, 2020 bailey1274, You can reduce the "reassign" loop quite considerably: ; Reassign our updated array back to our original variables For $var = 0 to UBound($VarArr) - 1 Step 2 Assign($VarArr[$var + 1], $VarArr[$var]) ; Assign value to correct name Next Not quite a single line - but rather more concise than your initial effort. M23 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
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