Jump to content
Sign in to follow this  
jefhal

How to remove empty array values

Recommended Posts

jefhal

Firstly, I am not posting my code as proof of descendancy from Rube Goldberg. That's already been established by my collection of over 1,000 Radio Shack connectors.

What I really would like input on is how to efficiently clean out empty array values. My code is an attempt at finding all valid MAC ID's on a computer. Usually, we have one per machine, but our laptops all have two. So, as I parse through the IPCONFIG /ALL output, I need to allow for either possiblity. (I temporarily am allowing for up to 6 MAC ID's, but that's just overkill.

So, the following code generates an array with one or two valid values (<>"0") and 4 or 5 "0" values:

#include <array.au3>
dim $onemac
dim $x = 0
dim $y = 6
dim $rawIPC[100]
dim $lineIPC[16]
Dim $onlyMacs[6]

ClipPut("")
RunWait(@comspec & ' /c ' & "ipconfig /all |clipboard")

$rawIPC = ClipGet()
;MsgBox(1,"",$rawIPC)
$splitIPC = StringSplit($rawIPC,@CRLF)

;_ArrayDisplay($splitIPC,"this is what's in the array")
;MsgBox(1,"",$splitIPC[0])
ClipPut("")

while $x < $splitIPC[0]
    if StringInStr($splitIPC[$x],"Phys") <> "0" Then
        $onemac = StringStripCR(StringStripWS(StringLeft(StringTrimLeft($splitIPC[$x],43),18),8))
        ;MsgBox(1,"lineIPC = :",$onemac)
        _ArrayAdd($onlyMacs,$onemac)
        ;_ArrayDisplay($onlyMacs,"Now onlymacs is:")
        ;MsgBox(1,"",_arrayminindex($onlyMacs,0))
    EndIf
        $x = $x + 1
wend
    
while   $y <> 0
    ;MsgBox(1,"$Y= ",$y)
    _ArraySort($onlyMacs,1)
    if $onlyMacs[$y] = 0 Then
        _ArrayDelete($onlyMacs, $y)
    ;if StringLen(_ArrayMin($onlyMacs,0,1)) = 17 then ExitLoop
    ;MsgBox(1,"array min= ",stringlen(_ArrayMin($onlyMacs,0)))
    ;_ArrayPop($onlyMacs)
    ;_ArrayDisplay($onlyMacs,"Here are the MacIDs:")
    EndIf
    $y = $y - 1
WEnd
    _ArrayDisplay($onlyMacs,"Here is the last MacID:")
Exit

I tried deleting array values by finding the index of the minimum value, but _arrayminindex saw "0" and "00-9a-6b-32-f1-ac" as both being valid "minimum" values. This meant that sometimes I was deleting a "0" array value and sometimes I was deleting a "00-9a-6b-32-f1-ac" value.

What I have above works, but it feels "ugly"...

Note: this code uses a program called clipboard.exe from: Synesis Software Feel free to use this at your own risk or just copy and paste the output from ipconfig /all into the variable $rawIPC in my code above for testing.


...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites
jefhal

_ArrayAdd($onlyMacs,$onemac)

Alternatively, could I create an array with no values: "dim $array[0]" and then resize the array each time I use _ArrayAdd? That way, if I find 3 mac id's my array is $array[3], and if I only find one mac id, my array is $array[1]? What function would I use to dynamically resize an array?


...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites
Valuater

_ArrayAdd($onlyMacs,$onemac)

Alternatively, could I create an array with no values: "dim $array[0]" and then resize the array each time I use _ArrayAdd? That way, if I find 3 mac id's my array is $array[3], and if I only find one mac id, my array is $array[1]? What function would I use to dynamically resize an array?

<{POST_SNAPBACK}>

look at this in help it has a full explination in code for resizing an array

ReDim

--------------------------------------------------------------------------------

Resize an existing array

ReDim $array[subscript 1]...[subscript n]

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
jefhal

ReDim

Sweet! Thank you for pointing that out. I kept looking for a command like _ArrayReDim...

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites
jefhal

Here's the final result:

; This script will find the local MAC ID's 
; If one or more ID's are for wireless NIC's they will be marked with the tag "wlan"
; The script will be edited to accept command line input. That will allow it to be added to Active Directory as a shell command.
; Written by Jeff Hall with help from Ite5000 and the AutoIT team 9/4/05


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

dim $onemac
dim $x = 0, $y = 6, $i = 0, $j = 0
dim $rawIPC[100], $lineIPC[16], $onlyMacs[1]
global $cmdOUT = ''
dim $description = " ~ "

;;; Send the output of ipconfig /all to a variable $rawIPC
$PID = Run("ipconfig /all", "","", $STDOUT_CHILD)
        ;MsgBox(1,"PID=",$PID)
While NOT @ERROR
    $cmdOUT &= StdoutRead($PID)
Wend
        ;MsgBox(1, "IPConfig Output:", $cmdOUT)
$rawIPC = $cmdOUT
        ;MsgBox(1,"",$rawIPC)

;;; Go through each line to find any MAC ID's and determine if the ID is for a wireless nic
$splitIPC = StringSplit($rawIPC,@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-3],"wireless",0) then $onemac = $onemac & " WLAN"
        ;MsgBox(1,"MAC ID is: ",$onemac)
        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
    $description = $description & $onlyMacs[$j] & " ~ "
    $j = $j + 1
WEnd

MsgBox(1,"Description =: ", $description)
ClipPut($description)
Exit

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

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  

×