n4rk0o Posted May 9, 2012 Share Posted May 9, 2012 Hello, I try to return multiple values from a function but I didn't find any example. In multiple forums, I read about ByRef, arrays, etc. What I want to do is to return values by a function or something else but I didn't find how. $strComputer = "127.0.0.1" $objWMIService = ObjGet("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & $strComputer & "\root\sms") $colComputers = $objWMIService.ExecQuery _ ("Select * from SMS_Client") Local $Components = ''" For $objComputer in $colComputers $Components = $objComputer.DisplayName MsgBox(0,"", @CRLF & "Installed Components : " & $Components & @CRLF ) Next I am a beginner so I don't have the keys to do that. Maybe someone can help me. Thanks a lot Link to comment Share on other sites More sharing options...
James Posted May 9, 2012 Share Posted May 9, 2012 You could store the return values in an array, the callee then loops through the returned array with the values you need. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
n4rk0o Posted May 9, 2012 Author Share Posted May 9, 2012 The same code in a function: GetSMSInfos ("127.0.0.1") Func GetSMSInfos ($strComputer) $objWMIService = ObjGet("winmgmts:" _ & "{impersonationLevel=impersonate}!" _ & $strComputer & "rootsms") ;~ $colComputers = $objWMIService.ExecQuery _ ;~ ("Select * from Win32_NTLogEvent") $colComputers = $objWMIService.ExecQuery _ ("Select * from SMS_Client") Local $Components = '' For $objComputer in $colComputers $Components = $objComputer.DisplayName Return $Components Next EndFunc I tried a lot of things but in that state, the script return the first value and not all of it. I would like to store all values in a variable (array or else) and return it but I didn't find how. Link to comment Share on other sites More sharing options...
Zedna Posted May 9, 2012 Share Posted May 9, 2012 (edited) #include <Array.au3> $result = GetSMSInfos ("127.0.0.1") $result = StringSplit($result, '|') _ArrayDisplay($result) Func GetSMSInfos ($strComputer) $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!" & $strComputer & "rootsms") ;~ $colComputers = $objWMIService.ExecQuery ("Select * from Win32_NTLogEvent") $colComputers = $objWMIService.ExecQuery ("Select * from SMS_Client") Local $Components = '' For $objComputer in $colComputers $Components &= $objComputer.DisplayName & '|' Next If $Components <> '' Then $Components = StringTrimRight($Components,1) Return $Components EndFunc Edited May 9, 2012 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
jchd Posted May 9, 2012 Share Posted May 9, 2012 AutoIt functions are not multi-valued, that is: return at most one value.So your problem turns into: how can we group several distinct values into one?As the posted answers show, the simplest and cleanest way to achieve that is to store the multiple values into one array.There are other ways you could use to perform a similar (but not identical) action:pass ByRef parameter(s) that the callee function may change, effectively changing them in the caller function scope.use global variables (dirty, messy, deprecated, don't do that)return a structure built from DllCallStructCreate: reserved for specific, technical use.Lookup the emphasized words in the help file to learn more. 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...
JohnOne Posted May 9, 2012 Share Posted May 9, 2012 I've used SetError also to return an extra 2 ints now and again. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
James Posted May 9, 2012 Share Posted May 9, 2012 I've used SetError also to return an extra 2 ints now and again.There is a hole in your logic. AndrewSchultz 1 Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
JohnOne Posted May 9, 2012 Share Posted May 9, 2012 How so James? If I return SetError(2664,2274,77592) am I not returning 3 values? AndrewSchultz 1 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jchd Posted May 9, 2012 Share Posted May 9, 2012 In this particular case, yes, technically, the caller may get them. But you can't pass back anything else than integers and doing so precludes using specific values for indicating error conditions in the callee function, restricting even more what you can "pass" back, not withstanding that you may have to introduce more error codes during the life of the function. So all in all, this isn't a generic enough way of returning user values. And it's dirty, hugly, bad practice. AndrewSchultz 1 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...
James Posted May 9, 2012 Share Posted May 9, 2012 Following on from jchd, it's not idealogical to send back return statements in the errors. Erroneous values should be for error values only. It's illogical and definitely not good practise to be passing back a non-error value in the error handles. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
BrewManNH Posted May 9, 2012 Share Posted May 9, 2012 @extended is used in some functions that return as successful as a valid value. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
James Posted May 9, 2012 Share Posted May 9, 2012 @extended is used in some functions that return as successful as a valid value.I'm sure you're not saying @extended is for errors, but others may think so:Extended function return - used in certain functions such as StringReplace. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
BrewManNH Posted May 9, 2012 Share Posted May 9, 2012 Well, @extended IS set with SetError, so to most people's thinking, it could be thought of as an error indicator, even though you can set it with SetExtended also (which I don't see all that often though there are examples to be found). If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
James Posted May 9, 2012 Share Posted May 9, 2012 Well, @extended IS set with SetError, so to most people's thinking, it could be thought of as an error indicator, even though you can set it with SetExtended also (which I don't see all that often though there are examples to be found).That's a good point.I'm still a firm believer in not setting return values to an error indicator. Using SetExtended is probably a better way to go. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
BrewManNH Posted May 9, 2012 Share Posted May 9, 2012 I would agree with you on that point, definitely. I had the same issue myself with a example script I made until it was suggested that returning an array was better than setting the error/extended values and returning those. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
n4rk0o Posted May 9, 2012 Author Share Posted May 9, 2012 Thank you for the answers. I will try it now Link to comment Share on other sites More sharing options...
JohnOne Posted May 9, 2012 Share Posted May 9, 2012 I am in agreement that it is bad practice, but I did say I have done it at times, and only to return ints. The logic in that has no holes If I needed to return a real error I might only set a negative number for example or some other value out of the bounds of what is expected. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
n4rk0o Posted May 10, 2012 Author Share Posted May 10, 2012 (edited) Hello, I just want to create a function like that but I would like to add a parameter to my function to change the argument in the loop For: #include <Array.au3> $result = GetSMSInfos ("127.0.0.1","Select * from SMS_Client", "ClientVersion") $result = StringSplit($result, '|') For $i = 1 to $result[0] Step 1 Msgbox(0,"",$result[$i]) Next Func GetClientInfos ($strComputer, $Query, $ParamExt) $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!" & $strComputer & "rootccm") $colComputers = $objWMIService.ExecQuery ($Query) Local $Info = '' For $objComputer in $colComputers $Info &= $objComputer.ClientVersion & '|' $Info &= $objComputer.$ParamExt & '|' << Like That Next If $Info <> '' Then $Info = StringTrimRight($Info,1) Return $Info EndFunc Is it possible ? Thank you Edited May 10, 2012 by n4rk0o Link to comment Share on other sites More sharing options...
Zedna Posted May 10, 2012 Share Posted May 10, 2012 Try this For $objComputer in $colComputers $Info &= $objComputer.ClientVersion & @TAB ; $Info &= $objComputer.$ParamExt & '|' $Info &= Execute('$objComputer.' & $ParamExt) & '|' Next Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
MilesAhead Posted May 11, 2012 Share Posted May 11, 2012 (edited) I am in agreement that it is bad practice, but I did say I have done it at times, and only to return ints.The logic in that has no holesIf I needed to return a real error I might only set a negative number for example or some other value out of thebounds of what is expected.This makes me think of C coding. Since the design decision was made to take advantage of side effects in C anything other than 0 felt like something was assigned, and so should be considered a boolean True. But then they started using lots of error codes in functions. But by then the die was cast(no pun intended.) So then they got stuck with returning non-0 for success, but if you got an error, then call GetLastError() to get the error number. Then it all got flipped around with COM and HRESULT back to 0 being no error. It's kind of funny in a way. Life would have been simpler if they built in exceptions way at the beginning. But what do I know? I like to slip in a "goto" now and then just to piss people off. Edited May 11, 2012 by MilesAhead My Freeware Page 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