An alternative approach split'ing on points preceded by a letter and followed by a value or vice versa.

use 5.014; use Data::Dumper; open my $inFH, q{<}, \ <<EOD or die $!; N260G02X142.05Y649.62I77.33J58.34H2M25 N265M20 N270G45 N275G01X304.78Y608.8C45.91M25 N280M20 N285G46C0 N290G03X324.39Y638.4I-69.58J67.4H2M25 N295M20 N300G45 N305G01X180.04Y592.89C326.5M25 N310X279.77Y586.78C26.5 N315X195.39Y584.57C336.5 N320X228.31Y579.58C355.92 EOD my @dataLines = map { { split m{(?x) (?<=[A-Z]) (?=[-\d]) | (?<=[-\d]) (?=[A-Z]) } +} } map { chomp; $_ } <$inFH>; close $inFH or die $!; print Data::Dumper->Dumpxs( [ \ @dataLines ], [ qw{ *dataLines } ] );

The output.

@dataLines = ( { 'M' => '25', 'J' => '58.34', 'Y' => '649.62', 'H' => '2', 'I' => '77.33', 'N' => '260', 'X' => '142.05', 'G' => '02' }, { 'M' => '20', 'N' => '265' }, { 'G' => '45', 'N' => '270' }, { 'G' => '01', 'Y' => '608.8', 'X' => '304.78', 'M' => '25', 'C' => '45.91', 'N' => '275' }, { 'M' => '20', 'N' => '280' }, { 'N' => '285', 'C' => '0', 'G' => '46' }, { 'Y' => '638.4', 'J' => '67.4', 'M' => '25', 'G' => '03', 'X' => '324.39', 'I' => '-69.58', 'N' => '290', 'H' => '2' }, { 'N' => '295', 'M' => '20' }, { 'G' => '45', 'N' => '300' }, { 'N' => '305', 'C' => '326.5', 'M' => '25', 'G' => '01', 'Y' => '592.89', 'X' => '180.04' }, { 'N' => '310', 'C' => '26.5', 'X' => '279.77', 'Y' => '586.78' }, { 'Y' => '584.57', 'X' => '195.39', 'C' => '336.5', 'N' => '315' }, { 'X' => '228.31', 'Y' => '579.58', 'N' => '320', 'C' => '355.92' } );

I hope this is helpful.

Cheers,

JohnGG


In reply to Re: Efficient split for alpha numeric pairs in a row by johngg
in thread Efficient split for alpha numeric pairs in a row by merrymonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.