develooper Front page | perl.beginners | Postings from February 2002

Re: unique elements in array

Thread Previous
Jeff 'japhy' Pinyan
February 26, 2002 08:05
Re: unique elements in array
Message ID:
On Feb 26, Simon K. Chan said:

>The solution to my question is probably dead easy, but the bioperl
>module I'm working on right now has sucked up all by brain juice! ;-)
>Let's say I have this array:
>@food = qw(milk ham eggs bread eggs milk);
>How can I write a script that sticks all UNIQUE elements (occurs only once) into another array?
>So, in the above case, @another_array would receive "ham" and "bread"

You'll want to use a hash:

  $count{$_}++ for @food;
  @unique = grep $count{$_} == 1, keys %count;

Hashes are the answer to many questions.

Jeff "japhy" Pinyan
RPI Acacia brother #734
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About