Map Array to Hash

  • map: make a has from array (thanks Jay)
Given an array:

my @keepthese = qw(
  account_code adult_qty arrival_date child_qty depart_date gds_sys
  group_code lang_code prop prune_level rate_code rate_type room_type
  seamless_bk_code sga
);

Here's the slow way to make a hash out of it:

my %keepthese;
foreach (@keepthese) {
   $keepthese{$_} = 1;
}

Here's a faster way:

my %keepthese = map { $_, 1 } @keepthese;
 
perl/map-array.txt · Last modified: 2005/09/22 21:52 by bw
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki