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


In reply to Re: removing a column by johngg
in thread removing a column by jn64024

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.