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

What I have is a text file with the following text that repeats but the data is different:
Subject: Lab Test Log #1946 Date: Thu, 1 Jun 2000 10:56:15 -0500 (CDT) From: JUDY F <judyf@elgiloy.com> To: HAMPSHIRE - KUR <hampsc@combmet.com> FAILED HV HRD. I'LL CONT. TEST @ SHERRY'S CLAY, UNLESS I HEAR FFM. YOU. THANKS JUDY ELP - HAMPSHIRE(21) 06-01-00 + 10:54:34 JUDY F TERM # 171 LAB TEST LOG - LAB #1946 P +AGE # 1 ---------------------------------------------------------------------- +---------- Date In 05-30-00 1:00pm In Initials JF Lab Test Type Final Tag # 116014 Customer In Comments RTR 10342 COIL B ---------------------------------------------------------------------- +---------- Status Non-Conforming Date Out 06-01-00 10:52am Out Initials JF Lot Code H88231C Out Comments @.0075 T 102538 Y 39121 E 70% HRD.83 HRB (*159HV1KG)7RA B 3.48 DL .25 @ S.L.PER SUS. IGA /DOES NOT MEET WARD HV HRD. ---------------------------------------------------------------------- +---------- ** ORDERS ** ---------------------------------------------------------------------- +---------- 21 12136-1 COMBINED METALS OF CHICAGO
The code that I have to read this file is the following:
#!/bin/perl $delimiter = ","; $i=0; while(<>) { chomp; if ($_ =~ /^Subject:/){ @subject[$i] = $_; @subject[$i] =~ s/^Subject:(\D+)(\d+)/$2/e; } ... ... (more if statements to select what I need) ... (the concatenating part between the words "Out Comments" and "----") if ($_ =~ /^Lot Code/){ @lotCode[$i] = $_; @lotCode[$i]=~ s/^Lot Code(\s+)(\w+)/$2/e; $i++; } open OUTPUT, ">>data.txt"; # $allvar=join(',', $tag,$lotCode); # $result=$allvar; } for ($j=0; $j < $i; $j++) { print OUTPUT "@subject[$j];@failed[$j];@monthi[$j]/@dayi[$j]/@yeari[ +$j];@routerval[$j];" . "@tag[$j];@montho[$j]/@dayo[$j]/@yearo[$j];@lotCode[$j];@comme +ntso[$j]\n"; print "@subject[$j];@failed[$j];@monthi[$j]/@dayi[$j]/@yeari[$j];@ro +uterval[$j];" . "@tag[$j];@montho[$j]/@dayo[$j]/@yearo[$j];@lotCode[$j];@comme +ntso[$j]\n"; }
Ok, how do I concatenate the lines between "Out Comments" and the first "----"? Thank you

Replies are listed 'Best First'.
Re: Better description of my concatenating problem
by I0 (Priest) on Feb 22, 2001 at 04:05 UTC
    $concat .= $_ if /^Out Comments/ .. /^----/
Re: Better description of my concatenating problem
by a (Friar) on Feb 22, 2001 at 10:54 UTC
    Well, more perlish way to do some of this:
    if ( /^Subject:\D+(\d+)/ ){ # if you can be sure of the '#' before the digits, use this # if ( /^Subject:.*#(\d+)/ ) not that it buys you much push @subject, $1; next; } # if /subject ...
    push is much better than your $i++ which may be getting messed up, the way your snippet looks. I'm guessing your open(OUTPUT ... is supposed to be outside the while {...}.

    Not as snazzy as the ever terse, er, tidy io's suggestion, but:

    if ( /\s*Out Comments\s*(.*)$/ ) { push @out_comments, $1; while (<>) { # quit on a blank line or ------- last if /^\s*$/ or /^\s*[-]+\s*$/; s/^\s*//; # get rid of spaces at the beginning push @out_comments, $_; } next; } # if /Out Comments/
    not the 'strictest' of methods (re-while-ing the <> is frowned on by pendants) but if you're confident of your data format, it works. I get the impression you expect only one msg per email, so your arrays aren't needed, so scalars might be better (that is, $i is usually ends up '1'). Just do "$subject = $1;" and "$out_comment .= $_;" for the inner while loop. You'll avoid typos by:
    $output_str = "@subject[$j];@failed[$j];@monthi[$j]/@dayi[$j]/@yeari[$ +j];@routerval[$j];" . "@tag[$j];@montho[$j]/@dayo[$j]/@yearo[$j];@lotCode[$j];@com +mentso[$j]\n"; # or, if you've gone to scalars $output_str = "$subject;$failed;$monthi/$dayi/$yeari;$routerval;" . "$tag;$montho/$dayo$/$yearo;$lotCode;$commentso\n"; print OUTPUT $output_str; print $output_str;
    And, last but definitely not least; perl -w/use strict, if you're gonna use the tools, use them *safely*. You'll be glad you did later on.

    a