Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to make an array from an array?

by Anonymous Monk
on Dec 06, 2003 at 14:23 UTC ( #312765=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I have array @all_names=(john, andy, bill,andy, john, julia, bill). What I'd like to do is make an other array based on the first array. Array called @names should have every name just one time: (john,andy,bill, julia)
Pasi

Replies are listed 'Best First'.
Re: How to make an array from an array?
by barrd (Canon) on Dec 06, 2003 at 14:47 UTC
    Straight from Perl Cookbook (well almost :-)
    my @all_names = qw/john andy bill andy john julia bill/; my %seen = (); my @names = grep { ! $seen{$_} ++ } @all_names; print join(' ', @names);
    This prints john andy bill julia, I'm sure you'll get other variations.

    Update 1: Fixed typos in code D'oh.

    Update 2: As TomDLux rightly pointed out, you may wish to sort the names alphabetically, if so just change line 3 to this:

    my @names = sort grep { ! $seen{$_} ++ } @all_names;
    Returning: andy bill john julia (which is what I meant to do originally but ... err... didn't)
Re: How to make an array from an array?
by TomDLux (Vicar) on Dec 06, 2003 at 14:50 UTC

    This is a frequently asked question, the Search window will find the answer for you.

    • If order doesn't matter, use the values as keys to a hash, then extract a list of the keys.
    • If order does matter, go through the list, copying elements which have not been seen yet. As each element is copied, mark it as seen by setting a hash element.
    • go through the original array, checking whether an element has been seen yet, unsplice-ing duplicates.

    Good luck with your homework.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: How to make an array from an array?
by Anonymous Monk on Dec 06, 2003 at 15:15 UTC
Re: How to make an array from an array?
by etcshadow (Priest) on Dec 07, 2003 at 05:02 UTC
    @unique_list = keys %{{map{($_=>1)} @non_unique_list}};

    ------------
    :Wq
    Not an editor command: Wq
Re: How to make an array from an array?
by Anonymous Monk on Dec 07, 2003 at 08:02 UTC
    Thank you all for bothering to answer. You have solved my problem.
    As you might have noticed :) english is not my native lanquage. So it's sometimes difficult to make searches because of not knowing the right expressions or words for something.
    Pasi

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://312765]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2022-06-29 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (96 votes). Check out past polls.

    Notices?