Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

maps implicit "$1-handling"

by tos (Deacon)
on May 02, 2003 at 20:27 UTC ( [id://255154]=perlquestion: print w/replies, xml ) Need Help??

tos has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, while trying out different possibilities for Anonymous Monks question Select data between a START and END pattern, i did
# cat mt #!/usr/bin/perl -w @test = ( "192.168.1.1 index.html index 5 seconds", "192.168.1.1 links.html links, index.html index 10 seconds ", "192.168.1.1 article1.html art1, article2.html art2, adpage 200 s +econds", ); foreach (map { /[\d\.]+\s(.+)\sseconds/i } @test) { print "Test Data: +$_\n" };
The result is what i have wanted.
# ./mt Test Data: index.html index 5 Test Data: links.html links, index.html index 10 Test Data: article1.html art1, article2.html art2, adpage 200
As often as not i had the feeling perl "knows" what i want to do in this case too. That's a fact i like it so much for. But actually i don't know why it does so because i expected to evaluate $1.

Replies are listed 'Best First'.
Re: maps implicit "$1-handling"
by diotalevi (Canon) on May 02, 2003 at 20:37 UTC

    There is no magic here and map() has no special knowledge of $1 or any other number variable. Regular expression matches in list context return the contents of any matched grouping. In your case you have exactly one capture group so m// returns $1 which becomes the return value of the map() block and viola!, map() uses $1. But only indirectly.

Re: maps implicit "$1-handling"
by dragonchild (Archbishop) on May 06, 2003 at 13:50 UTC
    Try a test case that uses two capture groups and you'll see what's really happening.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://255154]
Approved by Tanalis
Front-paged by halley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-03-29 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found