this will fail under certain circumstances. for instance:

my @non_unique = qw/carpet car pet/; # fails on 'car' and 'pet' my @non_unique = ("bob and sue","bob"); # fails on space seperated wor +ds
i could go on, but i think you get the point. if you don't want to alter the order of the items, and you want to use some perl 5 magic, you can use a pseudo-hash. before i go on, i must say that pseudo-hashes are considered a failed experiment, and will most likely not be suppored in Perl 6. the example below is simply to provide some enlightenment as to how they work (which i like.)

check Returning A Pseudo-Hash in Array Context for a little discussion on pseudo-hashes
for an offsite explanation on pseudo-hashes, find the link on japhy's homepage, http://www.crusoe.net/~jeffp/
also, check the documentation: fields and the Pseudo-hashes section of perlref

#!/usr/local/bin/perl -w use strict; $|++; # array of non-unique words my @non_unique = ('bob and sue', 'bobcat', 'carpet', 'bob', 'car', 'ca +rpet'); # create empty pseudo-hash # hash key is unique word, hash value is array index, array value is + count my $ph = [{},]; for(@non_unique) { if(exists $ph->{$_}) # if word already in hash, { $ph->{$_}++; # increment word count } else # else { $ph->[0]{$_} = @$ph; # set hash key to last array index (end + of array) $ph->{$_}++; # increment word count to 1 # or in one line... (a little obfuscated) # $ph->[$ph->[0]{$_} = @$ph]++; } # or on one line... (a little more obfuscated - not recommended) # exists $ph->{$_} ? $ph->{$_}++ : $ph->[$ph->[0]{$_} = @$ph]++; } # or on one line... (even more obfuscated - really not recommended) # map{exists$ph->{$_}?$ph->{$_}++:$ph->[$ph->[0]{$_}=@$ph]++}@non_uniq +ue; # array of unique words, same order as non-unique words # (the extraction procedure looks a little messy) # it says, for the keys of the hash section of the pseudo-hash, # sort by the value, ascending my @unique = sort { $ph->[0]{$a} <=> $ph->[0]{$b} } keys %{$ph->[0]}; # pretty formatted output of unique array and word count format SortedWordsAndCountHeader = Words Count . format SortedWordsAndCount = @<<<<<<<<<<<<<<<<<< @>>>> $_, $ph->{$_} . $^ = "SortedWordsAndCountHeader"; $~ = "SortedWordsAndCount"; write for @unique;
this produces the output

Words Count bob and sue 1 bobcat 1 carpet 2 bob 1 car 1

~Particle


In reply to using pseudo-hashes for unique lists (was Re: Re: Return a Unique Array) by particle
in thread Return a Unique Array by Anonymous Monk

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.