Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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


    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.


    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?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-07-13 22:08 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (252 votes), past polls