Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: assigning regex matches to variables

by lidden (Curate)
on May 28, 2011 at 20:10 UTC ( #907161=note: print w/ replies, xml ) Need Help??


in reply to assigning regex matches to variables

Change my $digits = $string2 =~ /(\d)+/; to my ($digits) = $string2 =~ /(\d)+/; and it will work. The parens changes the assingment from scalar to list context.


Comment on Re: assigning regex matches to variables
Select or Download Code
Re^2: assigning regex matches to variables
by JavaFan (Canon) on May 28, 2011 at 20:22 UTC
    Context is one thing, but the regexp needs to be fixed as well. It ought to be /(\d+)/, with the plus inside the parenthesis.
Re^2: assigning regex matches to variables
by genghis (Novice) on May 28, 2011 at 20:26 UTC

    Couldn't get that to work. Also tried

    my @digits = $string2 =~ /(\d)+/;

    which didn't work either. I guess I'm generally confused about whether the =~ operator actually assigns the result of the search; i.e. whether

    $var =~ /some_regex/;

    changes the value of $var.

      $var =~ /some_regex/; does not change the value of $var, it just searches through $var for the pattern /some_regex/.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2015-07-05 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls