in reply to map <IF> to hash with replacement

%hash = map { $_=lc($_); s/<tag>//; s/<\/tag>//; $_ => 1 } <IF>;

Replies are listed 'Best First'.
Re^2: map <IF> to hash with replacement
by vit (Friar) on Feb 05, 2010 at 00:38 UTC
    So in map we go from left to right ?

      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

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

      { $_=lc($_); s/<tag>//; s/<\/tag>//; $_ => 1 }

      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.

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

        Thanks a lot!!!