Sign in to follow this  
Followers 0
n4rk0o

Return multiple data through a function

21 posts in this topic

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 ;)

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by Zedna

Share this post


Link to post
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

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.


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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

#10 ·  Posted

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.

Share this post


Link to post
Share on other sites

#11 ·  Posted

@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 Gude
How 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

Share this post


Link to post
Share on other sites

#12 ·  Posted

@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.

Share this post


Link to post
Share on other sites

#13 ·  Posted

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 Gude
How 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

Share this post


Link to post
Share on other sites

#14 ·  Posted

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.

Share this post


Link to post
Share on other sites

#15 ·  Posted

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 Gude
How 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

Share this post


Link to post
Share on other sites

#16 ·  Posted

Thank you for the answers.

I will try it now

Share this post


Link to post
Share on other sites

#17 ·  Posted

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.

Share this post


Link to post
Share on other sites

#18 ·  Posted (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 by n4rk0o

Share this post


Link to post
Share on other sites

#20 ·  Posted (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 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.

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 by MilesAhead

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0