Jump to content

How to return two values from a function


jefhal
 Share

Recommended Posts

I'm embarrassed to show this code, but I need help in getting the function to return two values (the mac id and the value of $j):

#include-once
#include <array.au3>
#include <Constants.au3>

;_GetMacIDs("sysl025"); unremark this line for testing from Scite

Func _GetMacIDs($strComputer)
dim $onemac, $pid
dim $x = 0, $y = 6, $i = 0, $j = 0
dim $lineIPC[16], $onlyMacs[1], $MacID[3], $splitIPC[100]
Global $cmdOUT
Global $rawIPC, $Exprss
dim $rawExprss
Global $description, $NewDesc
dim $i, $j, $lineIPC, $cancel
Global $MacDesc = ""
dim $rawPing, $rawST, $ServTag
Global $MacID
dim $MacIDsReturn[4]
ClipPut("")

ToolTip("Getting MacID's of target computer")

;;; Check to see if machine is online
$var = Ping($strComputer,250)
If $var Then; if $var was not equal to zero --- also possible:  If @error = 0 Then ...

$PID = run(@comspec & ' /c ' & "\\student2\apps$\_bin\utils\psexec.exe \\" & $strComputer & " ipconfig /all","","",$STDOUT_CHILD)
            MsgBox(1,"$PID for ping=",$PID)
    $cmdOUT = StdoutRead($PID)
    $rawIPC = $cmdOUT
    MsgBox(1,"$cmdOUT=",$rawIPC)
    $splitIPC = StringSplit($cmdOUT,@CRLF)
    MsgBox(1,"$splitIPC[0]=",$splitIPC[0])
    While $x < $splitIPC[0]
        if StringInStr($splitIPC[$x],"Phys") <> "0" Then
            $onemac = StringStripCR(StringStripWS(StringLeft(StringTrimLeft($splitIPC[$x],StringInStr($splitIPC[$x],":")),18),8))
            MsgBox(1,"onemac=",$onemac)
            if StringInStr($splitIPC[$x-2],"wireless",0) or StringInStr($splitIPC[$x-2],"wlan",0) then $onemac = $onemac & " WLAN"
            ReDim $onlyMacs[$i + 1]
            _ArrayAdd($onlyMacs,$onemac)
                 _ArrayDisplay($onlyMacs,"Now onlymacs is:")
            $i = $i + 1
        EndIf
            $x = $x + 1
    WEnd

;;; Tidy up the results and send them to the clipboard with formatting
;ToolTip("Cleaning up the variables and concatenating them")
    _ArraySort($onlyMacs,1)
    _ArrayPop($onlyMacs)
    if $i > 1 and StringInStr($onlyMacs[1],"WLAN") then _ArraySwap($onlyMacs[0],$onlyMacs[1])
            ;_ArrayDisplay($onlyMacs,"Here are all of the MacIDs:")
    
    While $j < $i
        $MacID[$j] = $onlyMacs[$j]
        tooltip($onlyMacs[$j])
        sleep(3000)
        $j = $j + 1
        ;MsgBox(1,"$MacDesc and $j = ",$MacDesc & " " & $j)
    WEnd
    MsgBox(1,"$MacID=",$MacID[$j-1] & " " & $j-1)
        Return $MacID[$j-1]
        $MacID[$j] = $j
        Return $MacID[$j]

Else
    Msgbox(0,"Status","An error occured with number: " & @error)
    Exit
EndIf

Exit
EndFunc

Here's the script that I'm using (not successfully) to get the two values out of the function above:

#include <GetMacIDs.au3>
dim $var[5]
$strcomputer = "sys0298"
$var[5] = _GetMacIDs($strcomputer)
MsgBox(1,"Returned value =",$var)
Sleep(5000)

Ugly, but works (up to a point). By the way, if your computer has two mac id's (or more) this function should return all of those values...

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

a little above me but...

i thought it was like this

_GetMacIDs($strcomputer, $j)

and in the include

Return ($MacID[$j] , $j)

just a hobbyiest's guess

8)

It is a little above me too but Return can only have 1 parameter, so

either do Return($strcomputer & "*" & $j) where * could be any character that does not exist normally in the data ( I often use "ฮผ" from character map) and in your main code split string on this character.

or pass array by ref and let function modify the array

Link to comment
Share on other sites

You could also use one or two ByRef variables.

Func _GetMacID($computer, ByRef $retMacID, ByRef $retI)
   $retMacID = "00-00-01-00"
   $retI = 3
EndFunc

MsgBox(1, "Test _GetMacID","$retMacID:=" & $retMacID & ", $retI:=" & $retI)

Regards

Uten

Edited by Uten
Link to comment
Share on other sites

ByRef is beginning to sink in, but I believe I misled everyone by failing to mention this:

On some machines there will be only one mac id. On others there will be two, and on still others there MIGHT be three. Therefore, I was trying to pass an array back to the calling script from the function. That array should contain:

$arMAC[0]==> the number of mac id's found ($j - 1)

$arMAC[1] ==> the first mac id

$arMAC[2] ==> the second mac id

$arMAC[3] ==> the third mac id

.

.

.

$arMAC[N] ==> the last mac id

I could use is8591's idea and just do a loop and concatenate all mac id's with "~" to make a long string and then split the string in my main script, but I thought an array was the "cutest". At this point, either one is GREAT! If you can explain how to pass an array from the included function to the script that would be icing on the cake...
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

i love this one

$days = StringSplit("Sun,Mon,Tue,Wed,Thu,Fri,Sat", ",")
;$days[1] contains "Sun" ... $days[7] contains "Sat"


for $x = 1 to $days[0]
    MsgBox(0,"test"," day is " & $days[$x] & "  " )
Next

stringsplit is really easy to use

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

i love this one

stringsplit is really easy to use

I have to agree. I guess I just want to "understand" ALL of AutoIT, even though I have a working solution in hand. However, I have taken your advice and made a workable solution. Thank you and everyone else who helped (Uten, is8591)!
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

OK, So you want somthing like this

#include <Array.au3>
; Make some space
dim $arr[20]

Func _GetMacIDs(ByRef $arr, ByRef $lastItem)
 ; Get the Macs
   $arr[0] = "00-00-00-01"
   $arr[1] = "00-00-00-02"
   $arr[2] = "00-00-00-03"
   $arr[3] = "00-00-00-04"
   $lastItem = 4  ; Deside if you want to return the index or the item count
  
EndFunc
; Just to check
_ArrayDisplay($arr)

Now, you could also write it like this, and lett the function create the array. The cavete is that there is a deep copy of the array when it is assigned to the ByRef $arr variable. So the first code is faster. If you can decide how many mac addresses there are before you create the array this is maybe a better solution.

#include <Array.au3>
; Make some space
dim $arr[20]

Func _GetMacIDs(ByRef $arr)
 ; Get the Macs
   dim $macCount = 4
   dim $arrFoo[$macCount]
   $arrFoo[0] = "00-00-00-01"
   $arrFoo[1] = "00-00-00-02"
   $arrFoo[2] = "00-00-00-03"
   $arrFoo[3] = "00-00-00-04"
   $arr = $arrFoo  ;Makes a deep copy (new array) of $arrFoo to $arr.
EndFunc
; Just to check
_ArrayDisplay($arr)

The String concat, string split ius not so bad either but probably expensive in processing time. (As if you care on this kine of problem ;) )

Regards

Uten

Edited by Uten
Link to comment
Share on other sites

a workable solution for a function that returns the mac ids of a remote computer:

#include-once
#include <array.au3>
#include <Constants.au3>

; _GetMacIDs("localhost") ; unremark this line for testing from Scite

Func _GetMacIDs($strComputer)
dim $onemac, $pid
dim $x = 0, $y = 6, $i = 0, $j = 0
dim $lineIPC[16] , $splitIPC[100]
Global $onlyMacs[1]
Global $cmdOUT
Global $rawIPC, $Exprss
dim $rawExprss
dim $lineIPC, $cancel
Global $MacDesc = ""
dim $rawPing, $rawST, $ServTag
Global $MacID

ToolTip("Getting MacID's of target computer")

;;; Check to see if machine is online
$var = Ping($strComputer,250)
If $var Then; if $var was not equal to zero --- also possible: If @error = 0 Then ...

$PID = run(@comspec & ' /c ' & "\\student2\apps$\_bin\utils\psexec.exe \\" & $strComputer & " ipconfig /all","","",$STDOUT_CHILD); remark this line out for testing on local machine
; $PID = run(@comspec & ' /c ' & "ipconfig /all","","",$STDOUT_CHILD); unremark this line for testing on local machine
$cmdOUT = StdoutRead($PID)
$rawIPC = $cmdOUT
$splitIPC = StringSplit($cmdOUT,@CRLF)
While $x < $splitIPC[0]
    if StringInStr($splitIPC[$x],"Phys") <> "0" Then
        $onemac = StringStripCR(StringStripWS(StringLeft(StringTrimLeft($splitIPC[$x],StringInStr($splitIPC[$x],":")),18),8))
            if StringInStr($splitIPC[$x-2],"wireless",0) or StringInStr($splitIPC[$x-2],"wlan",0) then $onemac = $onemac & " WLAN"
        ReDim $onlyMacs[$i + 1]
        _ArrayAdd($onlyMacs,$onemac)
    ; _ArrayDisplay($onlyMacs,"Now onlymacs is:")
        $i = $i + 1
    EndIf
    $x = $x + 1
WEnd

;;; Tidy up the results and send them to the clipboard with formatting
    _ArraySort($onlyMacs,1)
    _ArrayPop($onlyMacs)
        if $i > 1 and StringInStr($onlyMacs[1],"WLAN") then _ArraySwap($onlyMacs[0],$onlyMacs[1])
    ; _ArrayDisplay($onlyMacs,"Here are all of the MacIDs:")
    
While $j < $i
    $MacID = $MacID & $onlyMacs[$j] & " รก " 
    sleep(3000)
    $j = $j + 1
WEnd
Return $MacID

Else
Msgbox(0,"Status","An error occured with number: " & @error)
Exit
EndIf

Exit
EndFunc
Edited by jefhal
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Here's a cleaned up version that passes an array with [0] being the number of mac id's and [1]... being the mac id's themselves. This solution requires the help of Sysinternals psexec.exe in order to perform an ipconfig on a remote machine. This is a marvelous utility. Perhaps someone else has a remote ipconfig util?

#include-once
#include <array.au3>
#include <Constants.au3>

; _GetMacIDs("remote_system"); unremark this line for testing from Scite

Func _GetMacIDs($strComputer)
dim $onemac = "", $pid
dim $pingreply[4] = ["Host is offline","Host is unreachable","Bad destination","Unknown error"]   
dim $x = 0, $i = 0
dim $splitIPC[100]
Global $onlyMacs[1]
Dim $cmdOUT = ""
Dim $rawIPC = ""

ToolTip("Getting MacID's of target computer: " & $strComputer)

;;; Check to see if machine is online
$var = Ping($strComputer,250)
If $var Then; if $var was not equal to zero --- also possible: If @error = 0 Then ...
; To test on domain network
     $PID = run(@comspec & ' /c ' & "\\student2\apps$\_bin\utils\psexec.exe \\" & $strComputer & " ipconfig /all","","",$STDOUT_CHILD)
; To test on local machine only
;$PID = run(@comspec & ' /c ' & "ipconfig /all","","",$STDOUT_CHILD); unremark this line for testing on local machine
        ; MsgBox(1,"$PID for ping=",$PID)
    $cmdOUT = StdoutRead($PID)
    $rawIPC = $cmdOUT
        ; MsgBox(1,"$cmdOUT=",$rawIPC)
    $splitIPC = StringSplit($cmdOUT,@CRLF)
        ; MsgBox(1,"$splitIPC[0]=",$splitIPC[0])
    While $x < $splitIPC[0]
        if StringInStr($splitIPC[$x],"Phys") <> "0" Then
            $onemac = StringStripCR(StringStripWS(StringLeft(StringTrimLeft($splitIPC[$x],StringInStr($splitIPC[$x],":")),18),8))
            ; MsgBox(1,"onemac=",$onemac)
            if StringInStr($splitIPC[$x-1],"wireless",0) or StringInStr($splitIPC[$x-1],"wlan",0) then $onemac = $onemac & " WLAN"
            if StringInStr($splitIPC[$x-2],"wireless",0) or StringInStr($splitIPC[$x-2],"wlan",0) then $onemac = $onemac & " WLAN"
            ReDim $onlyMacs[$i + 1]
            _ArrayAdd($onlyMacs,$onemac)
            ;_ArrayDisplay($onlyMacs,"Now onlymacs is:")
        $i = $i + 1
        EndIf
        $x = $x + 1
    WEnd

;;; Tidy up the results and format them
;ToolTip("Cleaning up the variables and concatenating them")
    _ArraySort($onlyMacs,1)
    _ArrayPop($onlyMacs)
        if $i > 1 and StringInStr($onlyMacs[1],"WLAN") then _ArraySwap($onlyMacs[0],$onlyMacs[1])
    _ArrayDisplay($onlyMacs,"Here are all of the MacIDs:")
    _ArrayInsert($onlyMacs,0,$i)
    _ArrayDisplay($onlyMacs,"Here is the array after the size is inserted at 0:")
    Return $onlyMacs
Else
    Msgbox(0,"Status",$pingreply[@error-1])
    Exit
EndIf

Exit
EndFunc

The code I use to test it with is:

#include <GetMacIDs.au3>
dim $var = ""
dim $strcomputer = ""
global $onlyMacs = ""
$strcomputer = "fred"
$var = _GetMacIDs($strcomputer)
_ArrayDisplay($onlyMacs,"This is the value in the calling script for $onlyMacs: ")
for $n = 1 to $onlyMacs[0]
    MsgBox(1,"Returned value =",_ArrayPop($onlyMacs))
Next
Exit
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...