in reply to Re: from array to hash with grep
in thread from array to hash with grep

OK, al that makes me understand map and grep better. I just realized that I want it a little bit different, I need a hash with the same keys but the values should be the whole string, so you would get:
$inp{A} = 'abc_A_bla' ; etc
Is this still possible with map or should I switch to a simpel foreach
foreach ( @inp ) { ($key) = $_ =~ /\w{3}_(\w)/ ; $inp{$key} = $_ ; }
?

Luca

Replies are listed 'Best First'.
Re^3: from array to hash with grep
by bart (Canon) on Jun 15, 2006 at 08:27 UTC
    It's still possible but you'll have to use something closer to what sonofason wrote. For each match, you need to create a ($key, $value) pair so the list constructed by map is more like ($key1, $value1, $key2, $value2, $key2, $value3) for the three values. That way, assigning to the hash will insert them as you want.

    Now the code you can use to do that, can read for example:

    @flat = map { /\w{3}_(\w)/ ? ($1, $_) : () } @inp;
    It'll create a pair like ("A", "abc_A_bla") for a match, and an empty list for no match.

    Assign to the hash, and you get:

    %inp = map { /\w{3}_(\w)/ ? ($1, $_) : () } @inp;
Re^3: from array to hash with grep
by davorg (Chancellor) on Jun 15, 2006 at 08:27 UTC
    my @inp = ("abc_A_bla", "abc_B_bla", "abc_C_bla"); my %inp = map { /\w{3}_(\w)/; $1 => $_ } @inp ; foreach ( keys %inp ) { print "$_ => $inp{$_}\n"; }

    Update: Better to use bart's version which includes checks for when the regex doesn't match.

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

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