in reply to Probably an easy map question

You could forget the chomp and split and do it with a regexp.

use Data::Dumper; my %h = map { /^([^=]+?)\s*=\s*(\S.*)/ } <DATA>; print Dumper \%h; __DATA__ a = fred b = 1 ignorethis c = ===

Output:

$VAR1 = { 'c' => '===', 'a' => 'fred', 'b' => '1' };

This works because the regexp evaluates to a list of the contents of the two captures. What's nice about this is that it elegantly handles values that have an equal sign in them. It also just plain skips over any line that doesn't fit the format.

Replies are listed 'Best First'.
Re^2: Probably an easy map question
by pobocks (Chaplain) on Jan 15, 2009 at 04:49 UTC

    Am I correct in thinking that this operates on DATA in slurp mode (i.e. reading all the lines in at once)? Or does this work like while (<X>) magic?

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

      Yes, it will read the entire input before doing any work. That's true of the OP too, and the other map-based solutions I see posted.

        Thanks! I suspected that was so, but I wasn't sure.

        for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";