unique elements in array

2002-02-26 Thread Simon K. Chan

Hi All,

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"

Many thanks for your time, Everybody!

=
#

Warmest Regards,
Simon K. Chan - [EMAIL PROTECTED]

"Great spirits have always encountered violent opposition from mediocre minds."  - 
Albert Einstein

__
Do You Yahoo!?
Yahoo! Sports - Coverage of the 2002 Olympic Games
http://sports.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: unique elements in array

2002-02-26 Thread Jeff 'japhy' Pinyan

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  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]