This is an easy problem to solve (and you can sort multiple ways with the same code) if you will use a hash (I'll note the link shows yet another way to do this kind of operation).

#!/usr/bin/perl use warnings; use strict; use Getopt::Long; use Pod::Usage; =head1 NAME unique.pl -- examines data and keeps the unique ones. =head1 SYNOPSIS unique.pl [options] Options: --help Brief help message --man Full documentation --first Keep the first one found rather than the last. =head1 DESCRIPTION unique.pl -- examines data and keeps the unique ones. Program can keep the first or the last one found. =cut my $help = 0; my $man = 0; my $first = 0; GetOptions( 'help|?' => \$help, man => \$man, first => \$first, ) or pod2usage(2); pod2usage( -exitval => 0, -verbose => 1 ) if $help; pod2usage( -exitval => 0, -verbose => 2, -noperldoc => 1 ) if $man; my %hash = (); while(<DATA>) { chomp; my ( $comp, $id, $status ) = split ( /\s+/, $_, 3 ); next if ( $comp =~ m/COMPUTER/ ); if ( $first ) { next if ( defined( $hash{$comp} ) ); } $hash{$comp} = [ $id, $status ]; } for ( sort keys %hash ) { printf "%s %s %s\n", $_, $hash{$_}->[0], $hash{$_}->[1]; } __DATA__ COMPUTER DISTRIBUTION_ID STATUS 30F-WKS `1781183799.xxxx1' IC--- 30F-WKS `1781183799.xxx11' IC--- ADM34A3F9 `1781183799.41455' IC---
and the results are:

C:\Code>perl unique.pl --help Usage: unique.pl [options] Options: --help Brief help message --man Full documentation --first Keep the first one found rather than the l +ast. C:\Code>perl unique.pl 30F-WKS `1781183799.xxx11' IC--- ADM34A3F9 `1781183799.41455' IC--- C:\Code>perl unique.pl --first 30F-WKS `1781183799.xxxx1' IC--- ADM34A3F9 `1781183799.41455' IC---

In reply to Re: File Manipulation - Need Advise! by dwm042
in thread File Manipulation - Need Advise! by nashkab

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.