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

(code) RE: extract uniques and sort (thanks. 1WTDI)

by ybiC (Prior)
on Sep 22, 2000 at 22:53 UTC ( #33681=note: print w/ replies, xml ) Need Help??


in reply to extract uniques and sort

Here's what I ended up with for extracting uniques and sorting.   I know it can be further simplified, but my non-programmer brain can somewhat grok it as is.   Thanks again, all!
    cheers,
    ybiC

#!/usr/bin/perl -w use strict; my $infile = shift; my $outfile = shift; my %seen; open (IN, "<$infile") or die "Can't open $infile RO: $!"; open (OUT, ">$outfile") or die "Can't open $outfile RW: $!"; foreach my $item (<IN>) { # parse $infile into a hash and remove + duplicate keys ++$seen{$item}; } print OUT sort keys %seen; # sort keys and save to $outfile close IN or die "Cannot close $infile: $!"; close OUT or die "Cannot close $outfile: $!";


Comment on (code) RE: extract uniques and sort (thanks. 1WTDI)
Download Code
Replies are listed 'Best First'.
RE: RE: extract uniques and sort (thanks. 1WTDI)
by Fastolfe (Vicar) on Sep 22, 2000 at 23:57 UTC
    You know, if this is all that your script does, you may want to consider using standard Unix tools sort and uniq:
    $ cat unsorted | sort | uniq >sorted.unique
    It will probably be a bit faster.
        I didn't realize 'sort' had a -u option. Does that work on Solaris? For some reason I thought I had read that 'sort' and 'uniq' were both required for that task, but now that I look, that's clearly not the case with at least Linux and OpenBSD. And since I was piping anyway, I thought I'd start it with a 'cat' just so the train of thought was easy to follow (first you start with the file, then you sort it, then you look for unique lines). *shrug*
      "consider std Unix sort and uniq..."

      Thanks for the suggestion Fastolfe.   I considered that but the snippet is the tail end of a (still modest) 95 line script using Net::Telnet::Cisco to query LAN switches for CDP neighbors.   Number of switches is under 500 at my current company, so performance isn't really an issue.

      Could still be a viable option, but I want do what as much as I can within the script itself, and without using system calls.
          cheers,
          ybiC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2015-07-31 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls