Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: how to get output

by Util (Priest)
on Feb 18, 2014 at 05:28 UTC ( #1075263=note: print w/replies, xml ) Need Help??

in reply to how to get output

    my $out = $Ora_code =~ /ORA\-\d{5}/
    my ($out) = $Ora_code =~ /(ORA\-\d{5})/

Adding the first set of parens causes $out to be treated as a one-element list. Without those parens, you have scalar context instead of list context; regex matches in scalar context return "truth" of the match instead of anything captured in the match. That Truth is represented as the "1" you are seeing.

Adding the second set of parens declares what data should be captured when the regex is evaluated during the match.

Replies are listed 'Best First'.
Re^2: how to get output
by dbashyam (Novice) on Feb 18, 2014 at 05:38 UTC

    Hi, thanks for your reply. I did change now but still it prints output as '1', if you see my print statement I want to get the output as ORA-12505 in this scenario, but still not getting it right.

    #!/usr/bin/perl my $org = ""; $Ora_code = 'Failed tthe following error: ORA-12505, TNS:listener does + not currently know of SID given in connect descriptor'; print $Ora_code; if ( my ($out) = $Ora_code =~ /ORA\-\d{5}/) { print "inside\n"; print $out; } else { print "(no match)\n"; }

      As Util correctly mentioned, you need to enclose what you want captured within parentheses, so your regex should be:


      Thanks for your help. I got it now

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2021-10-26 12:15 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (90 votes). Check out past polls.