in reply to Better way?

If you are looking to do specifically what you state in your first sentence (assuming your print statements are for debugging purposes), you can do something like:
my @new = qw(4 5 6 7); my %old = qw(1 a 2 b 3 c 4 d 5 f); my %new; $new{$_} = $old{$_} for @new;
   MeowChow                                   
               s aamecha.s a..a\u$&owag.print

Replies are listed 'Best First'.
(dkubb) Re: (2) Better way?
by dkubb (Deacon) on Feb 16, 2001 at 11:50 UTC

    MeowChow, here's another version using a hash slice instead of a for loop to assign the contents of one hash to another:

    my %old = ( 1 => a, 2 => b, 3 => c, 4 => d, 5 => f, ); my @keys = qw(4 5 6 7); #Keys to copy from the old to the new hash my %new; @new{ @keys } = @old{ @keys };

    In this case, the difference between using a for loop and a hash slice means a slight edge in speed during benchmarking. However, the larger the data set the wider the gap becomes.