in reply to map it back jack

It's quite possible, but probably not as neat and readable as the foreach() loop:
my %myhash; my $v; map { $v = $_; $v =~ s/_/ /g; $myhash{$_}=$v; } @elements;
Update: Hmm, why did I get dinged negative votes for this one? Is it considered bad to make the hash assignment in the map block?

Hot Pastrami

Replies are listed 'Best First'.
Re: Re: map it back jack
by chipmunk (Parson) on Dec 16, 2000 at 03:09 UTC
    Many people frown on using map and grep in a void context. Both commands are intended for building a list that is returned at the end; if you don't need the returned list from map or grep, it's cleaner to use foreach instead.
    my %myhash; foreach (@elements) { my $v = $_; $v =~ tr/_/ /; $myhash{$_} = $v; }
    UPDATE: runrig pointed out that the my $v can be moved inside the loop, which didn't occur to me as I was rewriting the map to a foreach. I've made that change now.
      Alrighty, thanks for the tip.

      Hot Pastrami
Re: Re: map it back jack
by swiftone (Curate) on Dec 16, 2000 at 03:12 UTC