?
Solved

perl regex match assignment on same line

Posted on 2010-11-10
5
Medium Priority
?
738 Views
Last Modified: 2012-05-10
How can I do this in one line
                        $myvar=~ m/(\d[\d.,]*)/;
                        $myvar= $1;
0
Comment
Question by:NewtonianB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Accepted Solution

by:
Ferrosti earned 1600 total points
ID: 34102514
$myvar = $1 if ($myvar=~ m/(\d[\d.,]*)/);

should do that.
0
 

Author Comment

by:NewtonianB
ID: 34102544
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
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 200 total points
ID: 34102950
Yes.  That is perfectly valid - it's no different than having nested ifs.
0
 
LVL 6

Assisted Solution

by:deiaccord
deiaccord earned 200 total points
ID: 34103207
You could also try using a substitution instead

$myvar = s/.*?((\d[\d.,]*).*?/$1/;
0
 
LVL 84

Expert Comment

by:ozo
ID: 34106697
($myvar) = $myvar=~ m/(\d[\d.,]*)/;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Six Sigma Control Plans

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question