• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 749
  • Last Modified:

perl regex match assignment on same line

How can I do this in one line
                        $myvar=~ m/(\d[\d.,]*)/;
                        $myvar= $1;
0
NewtonianB
Asked:
NewtonianB
3 Solutions
 
FerrostiCommented:
$myvar = $1 if ($myvar=~ m/(\d[\d.,]*)/);

should do that.
0
 
NewtonianBAuthor Commented:
What happens if this is in an if block that does matching itself?
if ($line=~ m/mymatch/) {
      $myvar = $1 if ($line=~ m/(\d[\d.,]*)/);
}

is that valid?
0
 
wilcoxonCommented:
Yes.  That is perfectly valid - it's no different than having nested ifs.
0
 
deiaccordCommented:
You could also try using a substitution instead

$myvar = s/.*?((\d[\d.,]*).*?/$1/;
0
 
ozoCommented:
($myvar) = $myvar=~ m/(\d[\d.,]*)/;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now