Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Perl $

by lddzjwwy (Novice)
on Jun 17, 2013 at 12:26 UTC ( #1039350=perlquestion: print w/ replies, xml ) Need Help??
lddzjwwy has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I got a question about the $ variables in Perl, if I have some codes like below:

if(/^\s*(.+)\s*/){ if($1 =~ /\[(\d+)\]/){ ... } }

Is the (\d+) inside the second if $1 or $2?.

It is only a simple example for figuring out what the second (\d+) should be. If there are some syntax errors pls ignore them.

Thanks a lot guys.

Comment on Perl $
Download Code
Re: Perl $
by roboticus (Canon) on Jun 17, 2013 at 12:32 UTC

    lddzjwwy:

    You can answer most simple questions like this by:

    Generally when you read the documentation, it will mention various things that you might not understand, so when you write your experiment, try to cover as many different types of cases as possible. This will help you understand the feature better as well as giving you some practice on testing boundary conditions in your programs.

    Update: Fixed links/formatting. Thanks to Happy-the-monk and ww for the catches.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Perl $1, $2, $3, $4
by Anonymous Monk on Jun 17, 2013 at 12:33 UTC

    Is the (\d+) inside the second if $1 or $2?.

    its a different regular expression, its a different m//atch operation

    capture groups are numbered per match operation

    so all these 4 match operations overwrite $1 /(.)/; $foo=~ /(.)/; $bar=~ /(.)/; $baz=~ /(.)/;

    As do these $one = qr/(.)/ ; m{$one}; $foo=~ m{$one}; $bar =~ m{$one}; $baz =~ m{$one};

    perlvar#$<_digits_> ($1, $2, ...) , perlrequick, perlretut

      Thanks dude, very clear explanation.

Re: Perl $
by ww (Bishop) on Jun 17, 2013 at 12:48 UTC

    Your use of escaped brackets in the second regex leaves me uncertain about your original data... because my first thought was that you misunderstood the use of a character class. But on second thoughts, and taking a wild guess what at what the data might be (a number inside square brackets, in the original string), this example may clarify:

    #!/usr/bin/perl use 5.016; use strict; use warnings; # 1039350 $_ = " this is a number [12345] "; if(/^\s*(.+)\s*/){ say "in outter 'if', \$1: $1"; $_ = $1; if($1 =~ /\[(\d+)\]/){ say "\t But in inner if, \$1 : $1"; } } =head output: in outter 'if', $1: this is a number [12345] But in inner if, $1 : 12345 =cut

    A couple points, beyond those made above:

    • In your title, "Perl $" is not particularly good shorthand for "Perl vars" as the $ sigil specifies only one of several kinds of variables
    • Including a data sample is usually useful, and often clears up ambiguities
    • perldoc perlre and company will be helpful.

    If you didn't program your executable by toggling in binary, it wasn't really programming!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1039350]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-12-29 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls