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

Hi guys,

I was taught this

my (%a, %b); @a{@a} = (); @b{@b} = (); delete @a{@b}; delete @b{@a}; @a = keys %a; @b = keys %b;

to get same elements out but if I want to retain the same elements and remove the different ones, is there a simple method like this?

Thanks.

Desmond

Replies are listed 'Best First'.
Re: Further question on removing elements from arrays
by Zaxo (Archbishop) on Aug 25, 2005 at 05:41 UTC

    You can use grep.

    my (%a, %b); @a{@a} = (); @b{@b} = (); @a = grep {exists $b{$_}} @a; @b = grep {exists $a{$_}} @b;
    Now @a and @b should be equal, containing only the common elements.

    Added - fixed missing curlies, tlm++.

    After Compline,
    Zaxo

Re: Further question on removing elements from arrays
by davidrw (Prior) on Aug 25, 2005 at 11:43 UTC