Hi Monks....again...

OK, so I've got an @ with several hundred elements, some of them "batch", some initials, some numbers, all different lengths. I need to extract only the unique elements, and discard double elements.

I discussed a bit ago in CB, was told to use cmp, I'm not understanding how to get the elements of the array, to iterate through $a and $b in a function, all several hundred of them.

So I went to the cookbook, to find a way to count them with a hash. This is what I found:

#!usr/bin/perl use strict; #use warnings; my %seen = ( ); my @uniq = ( ); my @list = "bob, bob, sue, sue"; my $item; my $element; foreach $item (@list) { if ($seen{$item}) { # same, don't grab } else{ # if we get here, we have not seen it before $seen{$item} = 1; push(@uniq, $item); } } foreach $element(@uniq) { print $element . "\n"; }

But it doesn't work. And I haven't even tried adding numbers and such in with the test array yet. Can someone please break this down for me like I'm a two year old...I have never understood the cmp routines in examples, because I don't get how to pass all the hundreds of elements in my @ into to $a and $b to be compared. I understand how it compares after that, just not that first part. Please help me, I'm starting to see green Matrix code running down my eyes....

Thanks koolgirl

"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man.." -- George Bernard Shaw


In reply to Extracting unique elements from array by koolgirl

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.