Sign in to follow this  
Followers 0
willywill

_ArrayTrim

9 posts in this topic

Hi all,

I have a text file that has an array of IP addresses followed by the hostnames, like so:

10.20.0.1 ACB-HOST01

10.20.0.2 ACB-HOST02

10.20.0.3 ACB-HOST03

My script can search and locate the IP address, but I need to copy only the hostname to the clipboard, so I can rename the computer hostname and description. How can I trim the array, so that only the hostname is copied to the clipboard?

Thanks!

Share this post


Link to post
Share on other sites



Use a 2D array.

$array[0][0]=10.20.0.1

$array[0][1]=10.20.0.2

$array[0][2]=10.20.0.3

$array[1][0]=ACB-HOST01

$array[1][1]=ACB-HOST02

$array[1][2]=ACB-HOST03


My Projects:Smart Icons

Share this post


Link to post
Share on other sites

Hi all,

I have a text file that has an array of IP addresses followed by the hostnames, like so:

10.20.0.1 ACB-HOST01

10.20.0.2 ACB-HOST02

10.20.0.3 ACB-HOST03

My script can search and locate the IP address, but I need to copy only the hostname to the clipboard, so I can rename the computer hostname and description. How can I trim the array, so that only the hostname is copied to the clipboard?

Thanks!

Many ways to skin that cat. Here's one:
#include <Array.au3> ; Only for _ArrayDisplay()

Global $avTest[3] = ["10.20.0.1 ACB-HOST01", _
    "10.20.0.2 ACB-HOST02", _
    "10.20.0.3 ACB-HOST03"]

_ArrayDisplay($avTest, "Before")

For $n = 0 to UBound($avTest) - 1
    $avTest[$n] = StringTrimLeft($avTest[$n], StringInStr($avTest[$n], " "))
Next

_ArrayDisplay($avTest, "After")

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Getting there. my only problem is that I don't want to define them in the script directly for reasons that the IP list will change over time. Here is what I have:

$file = FileOpen("C:\dhcp\list.txt", 0)
$IPAddress = String(@IPAddress1)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    Select
        Case @error = -1
            MsgBox (0, "Search Result", "End Of File was met")
            ExitLoop
        Case StringInStr($line, @IPAddress1)

            MsgBox (0, "Search Result", $line)
            ExitLoop
    EndSelect
WEnd

FileClose($file)

This works to a point of displaying the entire line that matches the IP address. Now I need to trim the fat to get to the hostname.

Share this post


Link to post
Share on other sites

Not meaning to oversimplify things but would StringSplit with a space delimter be super easy?

Share this post


Link to post
Share on other sites

Simplifying is actually a very good idea. I'm a noob, so I'm learning it as I go. StringSplit with a comma delimiter would probably work. Haven't tried it though.

Share this post


Link to post
Share on other sites

Something like:

$stdin = FileOpen("C:\dhcp\list.csv",0)
$stdout = FileOpen("C:\dhcp\listout.csv",2)
$host = String(@IPAddress1)

while 1
    $line = FileReadLine($stdin)
    if @error = -1 then ExitLoop
    if StringInStr($line,$host) Then
        $split = StringSplit($line, '\,', 0)
        FileWriteLine($stdout,$split[2])
        MsgBox(0,"Found",$host & " found:" & @crlf & $line & @CRLF & "result has been added to results.csv")
    EndIf
WEnd

Share this post


Link to post
Share on other sites

So does that mean it worked for you?

Share this post


Link to post
Share on other sites

It did! Awesome!

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