On Sep 21, Bob Showalter said:

>   my %hash = (
>      foo => 1,
>      bar => 2,
>      baz => 3,
>      qux => 4,
>   );
>
>I would like to remove all the entries in the hash except for 'bar' and
>'qux'. (Actual hash has other entries which can vary at runtime. I know that
>I only want to keep 'bar' and 'qux' however).

>    my @keys = qw(bar qux);

You could do:

  my %keep_these_keys;
  @keep_these_keys{qw( bar qux )} = ();

  delete @hash{ grep !exists $keep_these_keys{$_}, keys %hash };

-- 
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to