Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: read a word from a line

by atemon (Chaplain)
on Feb 25, 2009 at 14:17 UTC ( [id://746273]=note: print w/replies, xml ) Need Help??


in reply to read a word from a line

Hi,

Try :

while(<PAGE>) { if (/pond.*\((\w+)\)/) { print "$1\n"; } } close(PAGE);
output:
start end

$_ is the default scalar variable. In your while loop, when you read the file, you are NOT specifying any variable to hold the line read from file. So its kept in $_. Again for your regular expression, you are not specify any string/variable to match with. So it again match with $_. So when you print $_, it prints entire line. Again, $1 contains the first matching string. For details please have a look at perlre and perlvar

The above code is same as

while($_ = <PAGE>) { if ( $_ =~ /pond.*\((\w+)\)/) { print "$1\n"; } } close(PAGE);
or can be written without relying on $_ as
my $line; while($line = <PAGE>) { if ($line =~ /pond.*\((\w+)\)/) { print "$1\n"; } } close(PAGE);

Cheers !

--VC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://746273]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-04-18 02:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found