CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: [Laurent_R] Read the var values: Edit Log



Chupo_cro
Novice


Aug 19, 2012, 7:23 PM


Views: 7335
Re: [Laurent_R] Read the var values


Quote
Hello,

Am I correct understanding that:


Code
1234 X200.18Y50.41Z42.78


mean that X = 200.18, Y = 50.41 and Z = 42.78?


Yes, that is correct. The '=' is optional and optional spaces are allowed after the X, Y, or Z. The text is in fact G-code. However, different CNC machines may use different syntax, hence the '=' is optional. The lines I made for the testing purposes are purposely made to reflect as many as possible allowed variations.

Quote

Given that X, Y and Z are not always in the same order, I would probably use three distinct regexes on each line of input.

Assuming you line is in $_, something like this:


Code
$X = $1 if /X=?([\d]+)/; 
$Y = $1 if /Y=?([\d]+)/;
$Z = $1 if /Z=?([\d]+)/;


Thank you very much! I found this information very useful!!

Quote

And, BTW, it might seem costly, but using 3 simple regexes is not necessarily more expensive that a single complicated one that is far more likely to require a lot of backtracking.

The regexes above take into account an optional '=' sign between the letter and the numbers.

You could also decide to remove the equal sign before stating your matches:


Code
s/=//g; # removes the "=" characters 
$X = $1 if /X([0-9]+)/;
$Y = $1 if /Y([0-9]+)/;
# ...


Thank you very much for your help!
Regards
Chupo_cro

(This post was edited by Chupo_cro on Aug 19, 2012, 7:48 PM)


Edit Log:
Post edited by Chupo_cro (Novice) on Aug 19, 2012, 7:25 PM
Post edited by Chupo_cro (Novice) on Aug 19, 2012, 7:48 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives