in reply to Re: map <IF> to hash with replacement
in thread map <IF> to hash with replacement

So in map we go from left to right ?
  • Comment on Re^2: map <IF> to hash with replacement

Replies are listed 'Best First'.
Re^3: map <IF> to hash with replacement
by GrandFather (Saint) on Feb 05, 2010 at 01:06 UTC

    map 'returns' a result for each element processed. The result is the last value of the last statement in the map block and may be a list. Consider:

    #!/usr/bin/perl use strict; use warnings; my @lines = qw(<tag>A</tag> <TAG>B</TAG> <tag>C</tag>); for my $expr ( 'lc', 'lc($_), 1', '$_=lc($_); s/<tag>//; s/<\/tag>//; $_ => 2' ) { my @values = eval "map {$expr} \@lines"; print join("\n ", "map {$expr} =>", @values), "\n"; }

    Prints:

    map {lc} => <tag>a</tag> <tag>b</tag> <tag>c</tag> map {lc($_), 1} => <tag>a</tag> 1 <tag>b</tag> 1 <tag>c</tag> 1 map {$_=lc($_); s/<tag>//; s/<\/tag>//; $_ => 2} => a 2 b 2 c 2

    That result can be a list (as shown by almut


    True laziness is hard work
Re^3: map <IF> to hash with replacement
by almut (Canon) on Feb 05, 2010 at 00:45 UTC

    Yes.  Just like you normally go from top to bottom when the statements are written on separate lines:

    { $_=lc($_); s/<tag>//; s/<\/tag>//; $_ => 1 }
Re^3: map <IF> to hash with replacement
by ikegami (Patriarch) on Feb 05, 2010 at 07:19 UTC

    Statements are always executed from the earliest in the file to the latest in the file (unless overridden by loops, etc, of course). The statements in a map block are no exception.

Re^3: map <IF> to hash with replacement
by jwkrahn (Abbot) on Feb 05, 2010 at 00:46 UTC

    Yes, just like in a program we go from top to bottom.

      Thanks a lot!!!