Bob Showalter wrote:
Consider,

   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).

Here's what I'm doing:

    my @keys = qw(bar qux);
    my @values = @[EMAIL PROTECTED];
    %hash = ();
    @[EMAIL PROTECTED] = @values;

Is there a more elegant approach?

This is slightly shorter:

    %hash = map { $_, $hash{$_} } qw(bar qux);

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
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