Solved

perl regex match assignment on same line

Posted on 2010-11-10
5
736 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 400 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 50 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 50 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Convert grep lines to perl 6 80
Matching a random pattern with one common character 2 86
Regular expressions Exclude patter 3 46
html form to write data to csv 24 172
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 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