Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
We don't bite newbies here... much
 
PerlMonks  

How to make an array from an array?

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

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 09: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 09: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 10:15 UTC
Re: How to make an array from an array?
by etcshadow (Priest) on Dec 07, 2003 at 00: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 03: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
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.