From: Don Pich on
Here is my script:

#!/usr/bin/perl
use strict;
use warnings;
my $infile = 'Pannaway.txt';
my $outfile = 'BASIP.list';
my $line = '';
open( INFILE, "< $infile" ) or die "Can't open $infile : $!";
open( OUTFILE, "> $outfile" ) or die "Can't open $outfile : $!";
while ($line = <INFILE>){
if ($line =~ /BAS-/)
{
my $IP = '';
my $Address = '';
my $Name = '';
my $mac = '';
my $type = '';
my $J6 = '';
my $J7 = '';
my $J8 = '';
my $J9 = '';
my $J10 = '';
my $J11 = '';
my $J12 = '';
my $J13 = '';
my $J14 = '';
my $J15 = '';
my $J16 = '';
($IP,$Address,$Name,$mac,$type,$J6,$J7,$J8,$J9,$J10,$J11,$J12,$J13,
$J14,$J15,$J16) = split '\t',$line;
my @values = split('\.', $IP);
foreach my $val (@values) {
$val = sprintf("%02x", $val);
printf OUTFILE $val;
}
print OUTFILE "\n";
}
}
close (INFILE);
close (OUTFILE);

Here is the input file 'Pannaway.txt':

10.20.151.250 STTM.15.01 BAS-ADSL48R 00:0A:9F:40:67:05
3.2.1.28
10.20.151.248 STTM.16.01 BAS-ADSL48R 00:0A:9F:40:6D:19
3.2.1.28
10.20.145.252 STTM.41.01 CO BAS-ADSL48R 00:0A:9F:40:67:15
3.2.1.28
10.20.145.250 STTM.41.02 CO BAS-ADSL48R 00:0A:9F:00:E5:72
3.2.1.28
10.20.128.29 STTM.BAR.01 BAR-GE12 00:0A:9F:50:0D:38
02.02.00.22
10.20.128.22 STTM.BAR.02 BAR-GE12 00:0A:9F:50:5E:E9
02.02.00.22

Here is the output file BASIP.list:
0a1497fa
0a1497f8
0a1491fc
0a1491fa

** Notice the extra line after 0a1491fa

I realize that after the field "IP" is converted to hex, it gets out of
the "foreach" loop and then prints the carriage return to start the next
line in the file. Can I redo this script to not have it create that last
carriage return? Can I place it in another spot so that it doesn't
create the extra blank line?
From: ccc31807 on
On Mar 4, 11:17 am, Don Pich <dp...(a)polartel.com> wrote:
> I realize that after the field "IP" is converted to hex, it gets out of
> the "foreach" loop and then prints the carriage return to start the next
> line in the file.  Can I redo this script to not have it create that last
> carriage return?  Can I place it in another spot so that it doesn't
> create the extra blank line?

Print the newline before each value.

Don't forget that you can put your logic in a function and then call
the function. This way, you can do a priming read and write, and put
the newline first in the loop, like this:

data_func();
while (DATA)
{
print "\n";
data_func();
}

CC

From: Don Pich on
On Thu, 04 Mar 2010 08:33:31 -0800, ccc31807 wrote:

But won't putting the print line at the beginning give me an empty line
at the top?
From: ccc31807 on
On Mar 4, 11:36 am, Don Pich <dp...(a)polartel.com> wrote:
> On Thu, 04 Mar 2010 08:33:31 -0800, ccc31807 wrote:
>
> But won't putting the print line at the beginning give me an empty line
> at the top?

That's the purpose of the priming read (or write). Put your logic in
the function except for the new line. Call the function before the
loop, then enter the loop. Inside the loop, print the new line and
then call the function, like this:

function_call()
while {condition remains true)
{
print new line;
function call();
}

CC.
From: J�rgen Exner on
Don Pich <dpich(a)polartel.com> wrote:
>Here is my script:
>
>#!/usr/bin/perl
>use strict;
>use warnings;
>my $infile = 'Pannaway.txt';
>my $outfile = 'BASIP.list';
>my $line = '';
>open( INFILE, "< $infile" ) or die "Can't open $infile : $!";
>open( OUTFILE, "> $outfile" ) or die "Can't open $outfile : $!";
>while ($line = <INFILE>){
> if ($line =~ /BAS-/)
> {
> my $IP = '';
> my $Address = '';
> my $Name = '';
> my $mac = '';
> my $type = '';
> my $J6 = '';
> my $J7 = '';
> my $J8 = '';
> my $J9 = '';
> my $J10 = '';
> my $J11 = '';
> my $J12 = '';
> my $J13 = '';
> my $J14 = '';
> my $J15 = '';
> my $J16 = '';

This whole marathon above is unnecessary.
- for the declaration just add a "my" in front of the opening
paranthesis in the next line and Perl will declare all those variables
in the list in one single go.
- and the initializations are pointless, too, because you overwrite
those empty strings in the next statement without ever using them.

> ($IP,$Address,$Name,$mac,$type,$J6,$J7,$J8,$J9,$J10,$J11,$J12,$J13,
>$J14,$J15,$J16) = split '\t',$line;

You are never using any of those values except for $IP. Why are you
extracting and even naming them? There are several ways to get exactly
the one value you are interested in:
- split() returns a list, therefore you can index exactly the one value
you are looking for:
my $IP = (split/\t/)[0];

- as you happen do be interested in the first element only you can
simply just catch that one and let all the others fall into the bit
bucket:
my ($IP) = split/\t/;

- if you really need a dummy to catch some unwanted values you should
use 'undef' instead of a named variable:
($IP, undef, undef, $otherValue) = split ....
will make it obvious that you are interested in the first and fourth
value anly and are deliberately ignoring the second and third.
Ov course this could also be written as
my ($IP, $otherValue) = (split/\t/)[0,3];

- sometimes it is nice to use the split limit:
($IP, undef) = split /\t/, $_, 2;
makes it very obvious that I am interested in the first element only and
are deliberately ignoring whatever else is following in that line.

jue