in reply to Improve foreach with help of map?

A better map would be
my ($result) = map /(something)/, split /\n/, $content;

Of course, map can probably easily be avoided by using the following

my ($result) = $content =~ /(something)/;

It might require small changes to the regex pattern, though

Replies are listed 'Best First'.
Re^2: Improve foreach with help of map?
by mickep76 (Beadle) on Oct 09, 2009 at 14:53 UTC

    Hey that solved it, thx! Knew there was a shorter way of saying that :)

    The final code is as...

    my ($dummy, $dummy, $result) = map /(\d\d \w\w\w \d\d\d\d)/, split /\n +/, $content;

    Instead of.

    my $match = 0; my $result = undef; foreach(split /\n/, $content) { if(/End Date/) { $match = 1 } elsif($match && /(\d\d \w\w\w \d\d\d\d)/) { $result = $1; last; } }

    Thx again!

      my ($dummy, $dummy, $result) = ...;

      should be

      my (undef, undef, $result) = ...;

      There's also

      my $result = ( map /(\d\d \w\w\w \d\d\d\d)/, split /\n+/, $content )[2];

      But really, you want the faster and more accurate

      my ($result) = $content =~ /End Date.*?(\d\d \w\w\w \d\d\d\d)/s;

        Thx, that even makes it simpler. Perl Nirvana is in reach I tell you.

        Your last one worked like a charm.