in reply to Looking for alternative syntax for assigning variables based on regex matches
my ($x,$y) = $foo =~ /(hello)(world)/ or die "failed to match"; if ( my ($x,$y) = $foo =~ /(hello)(world)/ ) { print $x, $y; } if ( $foo =~ /(?<x>hello)(?<y>world)/ ) { print $+{x}, $+{y}; }
Update: The first two work like any other variable assignment, so ($foo) = $foo=~/(hello)/; works too of course. (Update 2: If assigning back to the same one variable, so does s/// and its variations.)
|
---|
Replies are listed 'Best First'. |
---|
In Section
Seekers of Perl Wisdom