dak1611 has asked for the wisdom of the Perl Monks concerning the following question:

I have a txt file that I am trying to parse into variables.... Line 1 = 0154.45 +02 13.56 +03 64.35 Line 2 = +09 23.12 +10 8798 I have written a regular expression to parse out the first line, but how do I continue the regular expression onto the 2nd line to extract variables...ie $1 thru $5 Here is the input file: B A1 L+44941 C0747 *D 01+0100. 02+2014. 03+0111. 04+1820. 05+13.14 06+344.1 07+62.83 08+1017. 09+16.56 10+1811. A1 L+44951 C5311 * And my partial code open(FH, "<$file"); @array = <FH>; close FH; open(OUT,'>','outfile.txt'); @array1 = @array3..$#array-4; print OUT @array1; close OUT; open (FILE, "/test/outfile.txt"); while (<FILE>) { print; ~ /^.*(0-90-90-9)..*(0-90-90-90-9)..*(0-90-90-9)..*(0-90-90-90-9)..*(0-90-9.0-90-9) .*(0-90-90-9.0-9) .*(0-90-9.0-9).*(0-90-90-90-9)/s; $id = $1; $year4 = $2; $doy = $3 ; $time = $4; $speed = $5; $ddir = $6; $temp = $7; $baro = $8; $windgust = $9; $gusttime = $10;

Replies are listed 'Best First'.
Re: Regular Expression ?
by AnomalousMonk (Archbishop) on Apr 21, 2014 at 20:16 UTC

    Ok, couldn't resist, here's some code (needs Perl version 5.10+ for \K):

    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; use Regexp::Common qw(number); ;; my @lines = ( qq{Line 1 = 0154.45 +02 13.56 +03 64.35 \n}, qq{Line 2 = +09 23.12 +10 8798 \n}, ); ;; my $prefix = qr{ \G (?: \A Line \s* \d+ \s* =)? \s+ }xms; for my $line (@lines) { my @numbers = $line =~ m{ $prefix \K $RE{num}{real} }xmsg; printf qq{'$_' } for @numbers; print ''; } " '0154.45' '+02' '13.56' '+03' '64.35' '+09' '23.12' '+10' '8798'

    Update: Of course,  \K isn't critical to this approach — I just like it so much... The expression
        m{ $prefix  ($RE{num}{real}) }xmsg
    (\K removed, parentheses  (...) around the  $RE{...} sub-expression) produces identical output for the given input, and I'm not aware of any version incompatibilities.

Re: Regular Expression ?
by AnomalousMonk (Archbishop) on Apr 21, 2014 at 19:19 UTC
Re: Regular Expression ?
by Anonymous Monk on Apr 21, 2014 at 19:07 UTC
    usually you simply use a loop ... show your code
Re: Regular Expression ?
by jellisii2 (Hermit) on Apr 22, 2014 at 12:21 UTC
      open(FH, "<$file"); @array = <FH>; close FH; open(OUT,'>','outfile.txt'); @array1 = @array[3..$#array-4]; print OUT @array1; close OUT; open (FILE, "/test/outfile.txt"); while (<FILE>) { print; ~ /^.*([0-9][0-9][0-9])..*([0-9][0-9][0-9][0-9])..*([0-9][0-9][0-9] +)..*([0-9][0-9][0-9][0-9])..*([0-9][0-9][.][0-9][0-9]) .*([0-9][0-9] +[0-9][.][0-9]) .*([0-9][0-9][.][0-9]).*([0-9][0-9][0-9][0-9])/s; $id = $1; $year4 = $2; $doy = $3 ; $time = $4; $speed = $5; $ddir = $6; $temp = $7; $baro = $8; $windgust = $9; $gusttime = $10;

      I appreciate your patience, as this is my first attempt at asking for help on this forum.

      My input file follows: B A1 L+44941 C0747 *D 01+0100. 02+2014. 03+0111. 04+1820. 05+13.14 06+344.1 07+62.83 08+1017. 09+16.56 10+1811. A1 L+44951 C5311 *