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

Re^3: write and read the same file not display the output

by Rudolf (Pilgrim)
on Sep 05, 2012 at 01:53 UTC ( [id://991720]=note: print w/replies, xml ) Need Help??


in reply to Re^2: write and read the same file not display the output
in thread write and read the same file not display the output

Hello ulaksh, Im guessing youve commented out your print "$_"; on purpose there. Anyway your getting yelled at because when the regex doesnt make a match, $a is not getting defined at all, so it is still undefined. You then try to use it on the next line print $a; You cant print $a because it has no value. It is undefined at the moment. This is also the reason your not getting anything printed to screen. maybe you can try this instead? :

print $1 if /^($name)$/;

Replies are listed 'Best First'.
Re^4: write and read the same file not display the output
by Kenosis (Priest) on Sep 05, 2012 at 02:07 UTC

    Good eye, Rudolf! Did you mean:

    print $1 if /^($name)$/;

      Hey thanks for the nice reply, but I was not aware of a difference in the regex with the parentheses (though I updated my post): It would be good if you can point it out for me, always up for learning more Perl ! :-)

      print $1 if /^$name$/; #or print $1 if /^($name)$/;

        $1 is initialized via regex captures which use parentheses. For example, in the following:

        my $string = 'Frank'; my $name = 'Frank'; print $1 if $string =~ /^$name$/; print $1 if $string =~ /^($name)$/;

        The first print statement produces:

        Use of uninitialized value $1 in...

        Whereas, the second print shows the value in $1:

        Frank

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found