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

Regex question

by Anonymous Monk
on Oct 14, 2008 at 09:44 UTC ( [id://716928]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi
why i can use this code:
use strict; use warnings; my $data = 'abcd 2001020713'; my @nums = $data =~ /\d+/g; print "$nums[0]\n";
which will print 2001020713
but not this one:
use strict; use warnings; my $data = 'abcd 2001020713'; my $number = $data =~ /\d+/g; print "$number\n";
which will print 1
isn't $nums[0] a scalar like $number ?? and in the first code the regex will assign the result to $nums[0] which is a scalar, why not we can use a simple scalar like $number?
thank you

Replies are listed 'Best First'.
Re: Regex question
by betterworld (Curate) on Oct 14, 2008 at 09:48 UTC

    This will work:

    my ($number) = $data =~ /\d+/g;

    The match operator returns the matched substrings only in list context, which is why your first code worked (assignment to an array). By using extra parentheses (like in my code above), you can turn a scalar assignment into a list assignment.

    Update: You won't need the /g flag if you use capturing parentheses:

    my ($number) = $data =~ /(\d+)/;
      (assignment to an array)

      I think you mean assignment to a list.

        Hmm, I meant this line of the OP's code:

        my @nums = $data =~ /\d+/g;

        I think it can be called an assignment to an array, which imposes a list context on the right-hand side, as it is described in perldata:

        Assignment to a scalar evaluates the right-hand side in scalar context, while assignment to an array or hash evaluates the righthand side in list context. Assignment to a list (or slice, which is just a list anyway) also evaluates the righthand side in list context.

        I think this would be an assignment to a list:

        my ($num) = ...
      Yeah, but it's actually the other way around. If you're using /g in list context, and only want to capture the entire match, you don't need the parenthesis. That is, if you have any capturing parenthesis, no additional parenthesis are implied.
        If you're using /g in list context, and only want to capture the entire match, you don't need the parenthesis.

        I don't think that this contradicts what I wrote.

        I was only suggesting to use parentheses rather than /g, because I wouldn't use the /g flag unless I really want to match several occurrences.

        (However, as Larry Wall put it: The argument against using an operator for other than its primary purpose strikes me the same as the old argument that you shouldn't have sex for other than procreational purposes. Sometimes side effects are more enjoyable than the originally intended effect.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-03-19 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found