Hi Jun,

Here is another way to solve your problem

[code]
@my_array = qw (one one two three three four);
my @unique_items = ();

foreach my $item (@my_array) {
        #grep in scalar context returns how many times $item is found in the 
@my_array
        my $count = grep $item eq $_, @my_array;
        #Add to array '@unique_items' only if $count is 1. This means ignore 
duplicates
        push @unique_items, $item if $count == 1;    
}

#print the unique items
print join ',', @unique_items;
[/code]

[output]
two,four

[/output]
 
best,
Shaji 
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------


________________________________
 From: Jun Meng <mengju...@gmail.com>
To: beginners@perl.org 
Sent: Sunday, 20 January 2013 8:39 AM
Subject: how to find item that happened once in an array
 
Hi Guys

I need to extract items that happened once from an array. Here is an example

@my_array=qw (one, one, two, three, three, four);

The expected result: @new_array=("two", "four").

Could you give me some suggestion? The hash could remove duplicates, and
return ("one", "two", "three", "four"), which is not I want. I just want
the items that don't have any duplicates.

Thanks

Jun
-- 
Jun Meng
257# marine science building
Department of Marine Science
University of Georgia
Cell Phone:1-706-248-2871
Office:1-706-542-1122

Reply via email to