in reply to Re^2: splitting directly to hash
in thread splitting directly to hash

Do you know what "map" does? If not, you might take a look at the documentation.

Let's split the code up a bit to make it easier to follow.

my $keys = '1 2 3 4 5 6'; # this splits your input data and stores the split # up elements in an array # So @key_list ends up containing # (1, 2, 3, 4, 5, 6) my @key_list = split /\s+/, $keys; # Here's where it gets interesting. # The map block is called once for every element in # the input list and returns the list that is created # by the block. # In this case each element is converted to a two-element # list, where the first element is taken from @key_list # and the second element is the value "undef". So, for # example, the first element from @key_list returns the # two-element list (1, undef). All of this little lists # are stored together in @mapped_list. So @mapped_list # will contain: # (1, undef, 2, undef, 3, undef, 4, undef, # 5, undef, 6, undef) my @mapped_list = map { $_ => undef } @key_list; # And finally we use standard list assignment to assign # our array to a hash. The even indexed elements (indexes # 0, 2, 3, etc) become the keys and the old indexed # elements (1, 3, 5, etc) become the values. # It's just like doing: # %hash = (1 => undef, 2 => undef, 3 => undef, ...) my %hash = @mapped_list;

Does that help at all?

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^4: splitting directly to hash
by narashima (Beadle) on Nov 21, 2006 at 17:42 UTC
    Thanks all. you guys are the best!!!