Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-26 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found