Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: assigning regex matches to variables

by lidden (Curate)
on May 28, 2011 at 20:10 UTC ( [id://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.

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://907161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found