Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: removing a column

by johngg (Canon)
on Jun 17, 2009 at 22:47 UTC ( [id://772567]=note: print w/replies, xml ) Need Help??


in reply to removing a column

It is a good idea to check for the success of your opens and closes and to use the three argument form for open with lexical filehandles. You should also use single quotes if you don't need interpolation.

my $hostfile = '/tmp/jn/hosts'; open my $hostFH, '<', $hostfile or die "open: < $hostfile: $!\n";
You need to take care when sorting IPs as a simple lexical sort will give odd results.

$ perl -le ' > @IPs = qw{ 22.32.87.12 56.67.38.61 101.12.34.54 }; > print for sort @IPs;' 101.12.34.54 22.32.87.12 56.67.38.61 $

I use sprintf here to form each quad into a fixed width string with leading zeros, making a 12-digit string that can be sorted lexically.

use strict; use warnings; my %seen; my @sortedUniques = map { ( split m{\s+}, $_->[ 0 ] )[ 0 ] } sort { $a->[ 1 ] cmp $b->[ 1 ] } grep { ! $seen{ $_->[ 1 ] } ++ } map { m{^(\d+)\.(\d+)\.(\d+)\.(\d+)\t} ? [ $_, sprintf q{%03d%03d%03d%03d}, $1, $2, $3, $4 ] : () } <DATA>; print qq{$_\n} for @sortedUniques; __END__ # hosts file 10.31.17.65 fw2 192.168.1.78 hosta 192.168.1.21 hostb 10.31.17.65 fw2dup 10.23.212.6 hostc 192.168.100.254 fw1 192.168.1.21 hostbdup

The output.

10.23.212.6 10.31.17.65 192.168.1.21 192.168.1.78 192.168.100.254

I hope this is helpful.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: removing a column
by poolpi (Hermit) on Jun 18, 2009 at 09:07 UTC

      All, Thank you for the information....I have taken all in consideration to finish off what I have.

      I love this language as it just shows above how many different ways the same thing can be completed along with all the considerations in error checking.

      Again thank you all very much!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://772567]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found