Okay

A number of things.

One: $data has not been defined. I think you want to look at $chunks[$index]

Two: The indices for @chunks goes from 1 to @chunks-1. So be careful!

Three: In your regular expression, you have captures within captures. Note that (abc(def)(ghi))(xyz) will match the following:

$1 = abcdefghi $2 = def $3 = ghi $4 = xyz
So, most of the time, your $5 returns undef.

Four: I am uncertain if you know what it is that you are matching. See the following code and results. It will show what you are matching, and what you could be matching if you used a simpler regex. I don't know if this is what you want, but it should get you started in the right direction.

Code

#!/usr/bin/perl use strict; use warnings; my @chunks = <DATA>; foreach my $i ( 0 .. @chunks-1 ) { my $data = $chunks[$i]; no warnings; if ( $data =~m/(\d+)\s{2,}(\d+) \s{2,}((\d|-)?(\d|,)*\.?\d*)\s{2,} +((\d|-)?(\d|,)*\.?\d*)\s{2,}/) { #/ ) { print "<$1> <$2> <$3> <$4> <$5> <$6> <$7> <$8>\n"; } } print "\n\n"; foreach my $i ( 0 .. @chunks-1 ) { my $data = $chunks[$i]; no warnings; if ( $data =~m/(\d+)\s+(\d+)\s+(-?\d*,?\d*\.?\d*)\s+(-?\d*,?\d*\.? +\d*)\s+/) { #/ ) { print "<$1> <$2> <$3> <$4>\n"; } } __DATA__ 1022289744 8008102935 221.00 + 199.00 70.50 3.20 0.00 + -9.70 27.09 290.09 1022290146 8008102942 0.00 + 199.00 63.80 0.00 0.00 + -3.80 26.70 285.70 1022290145 8008102930 0.00 + 199.00 207.80 3.20 1.20 + -120.00 30.04 321.24 1022289844 8008102943 0.00 + 199.00 5.50 9.00 0.00 + 0.00 21.98 235.48 1022290156 8008102954 0.00 + 199.00 283.40 0.40 11.20 + -51.80 45.53 487.73 1022290048 8008102949 0.00 + 199.00 0.00 0.00 0.00 + 0.00 20.50 219.50
Results
<1022289744> <8008102935> <221.00> <2> <1> <199.00> <1> <9> <1022290146> <8008102942> <0.00> <0> <> <199.00> <1> <9> <1022290145> <8008102930> <0.00> <0> <> <199.00> <1> <9> <1022289844> <8008102943> <0.00> <0> <> <199.00> <1> <9> <1022290156> <8008102954> <0.00> <0> <> <199.00> <1> <9> <1022290048> <8008102949> <0.00> <0> <> <199.00> <1> <9> <1022289744> <8008102935> <221.00> <199.00> <1022290146> <8008102942> <0.00> <199.00> <1022290145> <8008102930> <0.00> <199.00> <1022289844> <8008102943> <0.00> <199.00> <1022290156> <8008102954> <0.00> <199.00> <1022290048> <8008102949> <0.00> <199.00>
Good luck!

In reply to Re^5: parsing text file by Sandy
in thread parsing text file by doubledecker

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.