in reply to Scanning a file and extracting certains values within one or multiple lines

open(FILE1, $ARGV[0]) || die "Error: $!\n"; my $save=0; my $saveto=''; while (my $line=<FILE1>){ chomp $line; my @parts=split(' ',$line,2); if ( (!$save) && $parts[0] eq 'X1') { $saveto=$parts[1]; $save=1; } elsif ($parts[0] eq 'X2' ||($parts[0] eq 'BEGIN' && $parts[2] eq 'TAG' ) ){ print $saveto."\n" if ($saveto) ; $saveto=''; $save=0; } elsif ($save) { $saveto.=' '.$line;); } } print $saveto."\n" if ($saveto) ;
Im not gonna harp about lexical filehandles or 3 arg opens, somebody else can.

@lines = <FILE1>; "ate" the whole file at once. there was nothing left for the rest of your loop that wouldnt work anyway.

There better be a X2 or BEGIN TAG before a X3 or it will eat it too.

Replies are listed 'Best First'.
Re^2: Scanning a file and extracting certains values within one or multiple lines
by Arengin (Novice) on Mar 13, 2017 at 10:23 UTC
    Thanks for all the help.