0

I am reading from a text file with two columns. The first column contains the name of a device, and the second column contains the IP address for this device. how do i get the name and address and assign them to separate variable names?

2
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by bigfoot_80906
0

I am reading from a text file with two columns. The first column contains the name of a device, and the second column contains the IP address for this device. how do i get the name and address and assign them to separate variable names?

not really sure what you meant by two columns. Is it seperated by a tab?
a space? Or did you mean 2 rows?
I'll put in an example for both. ;)

###.seperated by a single space.###
#example name 123.123.123.123
open(fh,"<file") || die "error $!\n";
my @string;
my ($name,$ip);
         while(<fh>) {
         chomp;
         ($name,$ip)=split(/ /,$_);
         ##################
         #Process next
         }
################################
########seperated by a tab or more than 1 space ###########
while(<fh>) {
chomp;
s/\s/xmarkx/;
s/\s//g;
($name,$ip)=split(/xmarkx/,$_);
}

##############################

hope that helps

You can also go and read the perl manpage.
type "man perlretut" for regex examples and definition on linux.
on windows you can type >"perldoc perlretut"

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.