Hi all,
I am new to Perl but really want to learn to do some programming with networking stuffs. I have difficuty writing a Perl script that I wish somebody can help me with. The script I wrote basically read a data file into an array, change one variable with an "if-then" statement, put back in the array and print it out. It is as follows :
[CODE=perl]
#! /usr/bin/perl -w
open (src, 'aa') || die "cannot open this file $!";
open (dest, '>bb');
while (<src>) {
next if (/^[^0-9]/);
@tab = split(/\s+/,$_);
($o,$s,$t,$fo) = split(/\./,$tab[0]);
$test = 128;
if ($o ne $test) {
@table = ('1.1.1.1',@tab[1..13]);
} elsif ($o eq $test) {
@table = @tab;
}
print dest "@table\n";
}
close(src);
close(dest);
[/CODE]
When I ran it, I got warnings with "uninitiali zed value in an array" and the script only print out "1.1.1.1" on a line where the condition 1 is met and ignore "@tab[1..13]" and lines with initial read in @tab where condition 2 is met. I tried many different ways but still couldn't get what I want which is the same array as I first read in but the first column of the array will change depending upon condition 1.
I really appreciate anyone out there can help me with this little crazy trick somewhere in the script that I don't know of. Thanks again.
I am new to Perl but really want to learn to do some programming with networking stuffs. I have difficuty writing a Perl script that I wish somebody can help me with. The script I wrote basically read a data file into an array, change one variable with an "if-then" statement, put back in the array and print it out. It is as follows :
[CODE=perl]
#! /usr/bin/perl -w
open (src, 'aa') || die "cannot open this file $!";
open (dest, '>bb');
while (<src>) {
next if (/^[^0-9]/);
@tab = split(/\s+/,$_);
($o,$s,$t,$fo) = split(/\./,$tab[0]);
$test = 128;
if ($o ne $test) {
@table = ('1.1.1.1',@tab[1..13]);
} elsif ($o eq $test) {
@table = @tab;
}
print dest "@table\n";
}
close(src);
close(dest);
[/CODE]
When I ran it, I got warnings with "uninitiali zed value in an array" and the script only print out "1.1.1.1" on a line where the condition 1 is met and ignore "@tab[1..13]" and lines with initial read in @tab where condition 2 is met. I tried many different ways but still couldn't get what I want which is the same array as I first read in but the first column of the array will change depending upon condition 1.
I really appreciate anyone out there can help me with this little crazy trick somewhere in the script that I don't know of. Thanks again.
Comment