• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 743
  • 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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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