Sign in to follow this  
Followers 0
scriptnewbie

Importing text file into array

6 posts in this topic

I am new to autoit. I have a text file that has 2 columns (see sample below). Column 1 contains old ip addresses. Column 2 contains the new ip address to replace the old address. For example if ip address = 10.100.100.47, replace with 10.100.205.67. Would like to put in an array and be able to reference the old ip address and the new ip address. Basically saying if ipaddress = 10.100.100.47 replace with 10.100.205.67. Would someone please help me with the code for this. Thanks.

SAMPLE TEXT FILE :

old ip address new ip address

10.100.100.47 10.100.205.67

10.100.100.50 10.100.205.5

10.100.100.30 10.100.205.9

10.100.100.25 10.100.205.1

10.100.100.35 10.100.205.10

10.100.100.40 10.100.205.20

10.100.100.45 10.100.205.21

10.100.100.55 10.100.205.91

10.100.100.60 10.100.205.75

Share this post


Link to post
Share on other sites



Look at the example scripts for _FileReadToArray and StringSplit. Use a For/Next loop to go through the array, and StringSplit to get the parts of each line.

If you get stuck, post your code for more help.

:)


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

I added a comma in the text file as a separator. I am not that familiar with arrays. My code follows: How do I get the columns to variables. Thanks.

============================================

#include <file.au3>

; open file for reading

$readfile = "c:\ipaddr.txt"

;Read in lines of text into array until the EOF is reached

Dim $text

If Not _FileReadToArray("c:\ipaddr.txt", $text) Then

MsgBox(4096, "Error", " Error reading text file to Array error:" & @error)

Exit

EndIf

For $x = 1 To $text[0]

$array = StringSplit($text[$x], ",")

Next

; Close file

FileClose($readfile)

Share this post


Link to post
Share on other sites

#include <file.au3>

; open file for reading
$readfile = "c:\ipaddr.txt"


;Read in lines of text into array until the EOF is reached
Dim $text
If Not _FileReadToArray("c:\ipaddr.txt", $text) Then
    MsgBox(4096, "Error", " Error reading text file to Array error:" & @error)
    Exit
EndIf
;define 2 arrays - one for old IP's and the other for new IP's
Dim $old_ip[UBound($text)]
Dim $new_ip[UBound($text)]
;put the number of elements in the element 0 of each array
$old_ip[0] = $text[0]
$new_ip[0] = $text[0]

For $x = 1 To $text[0]
    $array = StringSplit($text[$x], ",")
    $old_ip[$x] = $array[1]
    $new_ip[$x] = $array[2]
Next 

; Close file

FileClose($readfile)

1 person likes this

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

You don't have to FileOpen/FileClose for _FileReadToArray(). It will do that for itself. And you were almost there. The $test array already has all the lines in it and $test[0] is the count. You create the 2D array (a 1D array is just a list, a 2D array is just a table with rows and columns) using $test[0] to tell you how big to make it. The loop reads each line from $test, splits into $array, and then copies the parts to the new array:

#include <file.au3>
#include <array.au3> ; only for _ArrayDisplay()

$sFile = "c:\Temp\ipaddr.txt"

;Read in lines of text into 1D array
Dim $text
If Not _FileReadToArray($sFile, $text) Then
    MsgBox(4096, "Error", " Error reading text file to Array error:" & @error)
    Exit
EndIf
_ArrayDisplay($text, "Debug: $text")

; Split lines into 2D array
Dim $avIPs[$text[0] + 1][2] = [["old ip address", "new ip address"]]
For $x = 1 To $text[0]
    $array = StringSplit($text[$x], ",")
    If $array[0] = 2 Then
        $avIPs[$x][0] = $array[1]
        $avIPs[$x][1] = $array[2]
    EndIf
Next
_ArrayDisplay($avIPs, "Debug: $avIPs")

:)


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

Thanks to all. Thanks for the tutorial on arrays.

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