Arengin has asked for the wisdom of the Perl Monks concerning the following question:
Hi.
I have the following code
#!/usr/bin/perl open(FILE1, $ARGV[0]) || die "Error: $!\n"; @lines = <FILE1>; while ($line=<FILE1>){ if ($line=~/^BEGIN_TAG/){ <FILE1>; #get next line if ($line=~/^X1/ .. /^X2/){ # get all between X1 and X2 print $line; # print all of it } } }
I need to get the values between X1 and X2 even if that is over multiple line and print them out concatenated (if oder more than 1 line)
However I'm not getting anything from the above....
The inputfile (inputtext.txt) looks like:
BEGIN_TAG X1 test test test X2 no no no no X3 yes yes yes BEGIN_TAG X1 test test test tes test test X2 no no no no no nono non no no nononono no no no X3 hi hi hi hi hi hi hi hi hi hi hi hi
Any ideas? I get for you this is trivial but I'm stuck at this a weekend over already.
Thanks for any help you can provide.
Arengin
Original contents restored above by GrandFather
Hi.
This is an edited question since the first part of the problem is solved
The code I have is:
#!/usr/bin/perl use strict; use warnings; open (FILE, '<', $ARGV[0]) or die "Could not open file: $!"; my $saveX1=0; my $saveX2=0; my $saveX3=0; my $savetoX1=''; my $savetoX2=''; my $savetoX3=''; while (my $line=<FILE>){ chomp $line; my @parts=split(' ',$line,2); if ( (!$saveX1) && $parts[0] eq 'X1') { $savetoX1=$parts[1]; $saveX1 +=1; } elsif ($parts[0] eq 'X2' ||($parts[0] eq 'BEGIN' && $parts[2] eq 'TA +G' ) ){ #print $savetoX1."\n" if ($savetoX1) ; $savetoX1=''; $saveX1=0; } elsif ($saveX1) { $savetoX1.=' '.$line; } if ( (!$saveX2) && $parts[0] eq 'X2') { $savetoX2=$parts[1]; $saveX2 +=1; } elsif ($parts[0] eq 'X3' ||($parts[0] eq 'BEGIN' && $parts[2] eq 'TA +G' ) ){ #print $savetoX2."\n" if ($savetoX2) ; $savetoX2=''; $saveX2=0; } elsif ($saveX2) { $savetoX2.=' '.$line; } if ( (!$saveX3) && $parts[0] eq 'X3') { $savetoX3=$parts[1]; $saveX3 +=1; } elsif ($parts[0] eq 'BEGIN' && $parts[2] eq 'TAG' ){ #print $savetoX3."\n" if ($savetoX3) ; $savetoX3=''; $saveX3=0; } elsif ($saveX3) { $savetoX3.=' '.$line; } } print "X1: ".$savetoX1."\n" if ($savetoX1) ; print "X2: ".$savetoX2."\n" if ($savetoX2) ; print "X3: ".$savetoX3."\n" if ($savetoX3) ;
The values of the print at the end are all messed up here is what I have and what I sould have:
The wrong one (the one I have) I get is: <br/> <code> X3: yes yes yes BEGIN_TAG X1 test test test tes test test X2 no + no no no no nono non no no nononono no no no X3 hi hi hi hi hi hi + hi hi hi hi hi hi
X1: test test test test test test tes test test X2: no no no no no no no no no nono non no no nononono no no no X3: yes yes yes hi hi hi hi hi hi hi hi hi hi hi hi
BEGIN_TAG X1 test test test X2 no no no no X3 yes yes yes BEGIN_TAG X1 test test test tes test test X2 no no no no no nono non no no nononono no no no X3 hi hi hi hi hi hi hi hi hi hi hi hi
Any ideas? I get for you this is trivial but I'm stuck at this a weekend over already.
Thanks for any help you can provide.
Arengin
|
|---|