Re: file script and cmd script difference



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
.



Relevant Pages

  • Re: next if question
    ... How can you have the parsed info printed? ... > use strict; ... > use warnings; ...
    (perl.beginners)
  • Re: umlautproblem
    ... da hab ich wohl das bastel-script geposted. ... use strict; ... use warnings; ... my $header; ...
    (de.comp.lang.perl.misc)
  • file script and cmd script difference
    ... I'd like to ask explaination/help regarding this: ... use strict; ... use warnings; ... my %macs; ...
    (perl.beginners)
  • Re: Number or string?
    ... >following error message: ... it seems you didn't include header line in you sample here. ... >#use strict; ... >use warnings; ...
    (perl.beginners)
  • Re: Sharing variables between modules
    ... use strict; ... use warnings; ... # Note that this must also come *before* the use Foo line. ... package Foo; ...
    (comp.lang.perl.misc)