in reply to Keeping order, no duplicate and the complete keyset

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.

Replies are listed 'Best First'.
Re^2: Keeping order, no duplicate and the complete keyset
by ikegami (Patriarch) on Dec 07, 2005 at 22:46 UTC

    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.