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

Hey People!

I have the following little algorithm:
use strict; my @big = qw(a b c d e f g h i); my @small = qw(f c d g); my @keys_wanted = @small; foreach (@big) { my $found = 0; foreach my $already_there (@small) { if ($already_there eq $_) { $found = 1; last; } } if (!$found) { push (@keys_wanted, $_); } } print "@keys_wanted";
This works, but Im sure there is some neater (perlish?), and maybe even some faster way to do the same?

I want it to, as you may see, have the keys in the order as mentioned in @small. And, the rest of the keys from @big. Maybe there is even a way to get rid of @keys_wanted?

Thanks,
Ace

Replies are listed 'Best First'.
Re: Keeping order, no duplicate and the complete keyset
by Roy Johnson (Monsignor) on Dec 07, 2005 at 21:51 UTC
Re: Keeping order, no duplicate and the complete keyset
by BrowserUk (Patriarch) on Dec 07, 2005 at 21:48 UTC

    One way

    perl> @big = qw(a b c d e f g h i);; perl> @small = qw(f c d g);; perl> undef @found{ @small };; perl> @wanted = ( @small, grep{ !exists $found{ $_ } } @big );; perl> print @wanted;; f c d g a b e h i

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      The above assumes there are no dups in @big:

      my @big = qw( a b c d e f g h i a ); my @small = qw( f c d g ); my %found; undef @found{@small}; my @wanted = ( @small, grep{ !exists $found{ $_ } } @big ); print("@wanted\n"); # f c d g a b e h i a

      The OP's version removed dups fom @big, and so does the following version:

      my @big = qw( a b c d e f g h i a ); my @small = qw( f c d g ); my %found; $found{$_}++ foreach @small; my @wanted = ( @small, grep{ !($found{$_}++) } @big ); print("@wanted\n"); # f c d g a b e h i

        Agreed. Your version can be simplified a little though.

        my @big = qw( a b c d e f g h i a ); my @small = qw(f c d g c); my %seen; my @wanted = grep{ !$seen{ $_ }++ } @small, @big; print "@wanted"; f c d g a b e h i

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Keeping order, no duplicate and the complete keyset
by planetscape (Chancellor) on Dec 08, 2005 at 07:56 UTC
Re: Keeping order, no duplicate and the complete keyset
by NetWallah (Canon) on Dec 08, 2005 at 05:58 UTC
    This uses hashes, and perl idioms:
    my @big = qw(a b c d e f g h i); my @kw= qw(f c d g); my %small = map {$_=>undef} @kw; exists $small{$_} or push @kw,$_ for @big; # or, Alternatively, # push @kw,grep {!exists$small{$_}} @big; print qq(@kw\n); -- Output -- f c d g a b e h i

         You're just jealous cause the voices are only talking to me.

         No trees were killed in the sending of this message.    However, a large number of electrons were terribly inconvenienced.

Re: Keeping order, no duplicate and the complete keyset
by tphyahoo (Vicar) on Dec 08, 2005 at 11:48 UTC
    I did it myself before peeking at the above answers, and came up with something that avoids grep and seems pretty concise to me, here it is:
    use strict; use warnings; my @big = qw(a b c d e f g h i); my @small = qw(f c d g); my %small; $small{$_}=1 for @small; my @keys_wanted = @small; foreach ( @big ) { push @keys_wanted, $_ unless $small{$_}; } print "@keys_wanted";
    This doesn't remove dupes though (see ikegami's comments above), and I'm not sure if it can be simply modified to do so.
Re: Keeping order, no duplicate and the complete keyset
by Ace128 (Hermit) on Dec 08, 2005 at 21:18 UTC
    Alrighty folks! Thanks for feedback! Can always count on you here! :)