For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2006 > file script and cmd script difference









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author file script and cmd script difference
Joseph

2006-06-29, 3:57 am

Hi list,

I'd like to ask explaination/help regarding this:

1)perl -e '%hash; for (`arp`) {($addr,$mac) = (split(/\s+/))[0,2];
$hash{$addr}= $mac } foreach (keys %hash) { print "\t $_ => $hash{$_}\n";}'
it emits the output which i'm hoping to see ex: wrkstation => MACADDR;
but this:
2)
#!/usr/bin/perl

use strict;
use warnings;

my %macs;

for (`arp`) {
my($addr,$mac) = (split(/s+/))[0,2];
$macs{$addr} = $mac;
}

foreach my $key (keys %macs) {
print "\t $key => $macs{$key}\n";
}

It's a doesn't produce the same thing, why?

Again thanks in advance.

/joseph


Jeff Peng

2006-06-29, 3:57 am



> my($addr,$mac) = (split(/s+/))[0,2];


You maybe would check your scripts carefully at first.
Here should be:
my($addr,$mac) = (split(/\s+/))[0,2];

You have lost the '' before 's+' in your regex.


John W. Krahn

2006-06-29, 3:57 am

joseph wrote:
> Hi list,


Hello,

> I'd like to ask explaination/help regarding this:
>
> 1)perl -e '%hash; for (`arp`) {($addr,$mac) = (split(/\s+/))[0,2];
> $hash{$addr}= $mac } foreach (keys %hash) { print "\t $_ => $hash{$_}\n";}'


Could be written more simply as:

arp | perl -lane'print "\t $F[0] => $F[2]"'

And if you don't want the header line:

arp | perl -lane'print "\t $F[0] => $F[2]" if $. != 1'


> it emits the output which i'm hoping to see ex: wrkstation => MACADDR;
> but this:
> 2)
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my %macs;
>
> for (`arp`) {
> my($addr,$mac) = (split(/s+/))[0,2];


You are telling split() to use one or more of the letter 's' to split on.
Better to use the default split behaviour:

my($addr,$mac) = (split)[0,2];


> $macs{$addr} = $mac;
> }
>
> foreach my $key (keys %macs) {
> print "\t $key => $macs{$key}\n";
> }



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com