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?

I don't know if it is more elegant but you could do it like this:

%hash = map @$_, grep $_->[0] =~ /^(?:bar|qux)$/, map [ $_, $hash{$_} ], keys %hash;

:-)

John
--
use Perl;
program
fulfillment

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