Here's another, slightly different approach. It tries to factor regex elements, and to build an extraction regex (rather than a split pattern of some kind) based on a mapping of all valid commands to their possible values. I make some assumptions:
-
an $integer is zero or positive, never negative (update: and it never has a sign);
-
certain real numbers like .0 or 0. need not be recognized;
-
some commands like N G have $integer values only, and others like X Y have only $real values (the OP says a command is "a letter followed by a number which can be either an integer or real", implying that N, for instance, could have a real value);
-
there is no inherent order in the occurrence of the N G X Y etc. commands in a command sequence.
Note that the command sequence is validated before commands are extracted (the undefined
'C' command in one of the example sequences causes an abort).
Also note that I use the
(?|pattern) extension of Perl 5.10+ (see
Extended Patterns in
perlre); if you need to avoid this construct, I can provide an alternative.
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le
"my $integer = qr{ (?<! \d) \d+ (?! \d) }xms;
my $real = qr{ -? $integer [.] $integer }xms;
;;
my %cmds = (
N => $integer, G => $integer, H => $integer, M => $integer,
X => $real, Y => $real, I => $real, J => $real,
);
;;
my ($rx_cmd) =
map qr{ (?| $_) }xms,
join q{ | },
map qq{($_) ($cmds{$_})},
keys %cmds
;
;;
for my $s (qw(
N260G02X142.05Y649.62I77.33J58.34H2M25
N265M20
N270G45
N290G03X324.39Y638.4I-69.58J67.4H2M25
N275G01X304.78Y608.8C45.91M25
N280M20
)) {
print qq{seq: '$s'};
die qq{bad: '$s'} unless $s =~ m{ \A $rx_cmd+ \z }xms;
my %parts = $s =~ m{ $rx_cmd }xmsg;
dd \%parts;
}
"
seq: 'N260G02X142.05Y649.62I77.33J58.34H2M25'
{ G => "02", H => 2, I => 77.33, J => 58.34, M => 25, N => 260, X => 1
+42.05, Y => 649.62 }
seq: 'N265M20'
{ M => 20, N => 265 }
seq: 'N270G45'
{ G => 45, N => 270 }
seq: 'N290G03X324.39Y638.4I-69.58J67.4H2M25'
{ G => "03", H => 2, I => -69.58, J => 67.4, M => 25, N => 290, X => 3
+24.39, Y => 638.4 }
seq: 'N275G01X304.78Y608.8C45.91M25'
bad: 'N275G01X304.78Y608.8C45.91M25' at -e line 1.
Give a man a fish: <%-(-(-(-<
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.