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

Hi all, I am struggling to write a program in which i have to read 2 diferent type of loops from many .doc files. These files consist of data as
data1; if(some condition1 = 1) then read this data 2; end if; if (some condition2 = 0) then read this data 3; read data 4; end if; if(some condition3 = 11) then read this data 5; data 6; end if; if (some condition2 = 0) then read this data 6; end if;
from which i have to read some data I am trying to check only for condition 2 and thus to get the output:
read this data 3; read data 4; read this data 6;
I wrote a prog to read the only condition 2 and try to read multiple files as:
open(WORDLIST, <*.doc>); while(<WORDLIST>) if(/if/) { if(/condition 2/) {print "$_"; } else{} } open(WORDLIST);
Am not able to read the desired output lines nor able to read multiple files. If a write a file name eg open(WORDLIST, <test.doc>); then am able to access one file at a time. Could u guys please help me.. Thanking u in advance...

Replies are listed 'Best First'.
Re: read different loops
by toolic (Bishop) on Nov 17, 2008 at 20:01 UTC
    <*.doc> returns a list of file names (see glob). If your current directory has more than one file with a .doc extension (i.e., a.doc, b.doc, c.doc), then more than one file name will be passed to open. You only want to pass 1 file name at a time to open. Also, you always want to check the success of open:
    my @docs = <*.doc>; for my $file (@docs) { open my $fh, '<', $file or die "can not open file $file: $!"; while {<$fh>} ( # process your file here ) close $fh; }
Re: read different loops
by Perlbotics (Archbishop) on Nov 17, 2008 at 20:15 UTC

    Maybe a simple state machine?

    use strict; my $in_section = 0; foreach (<>) { $in_section=1, next if m{if\s+\(some\s+condition2\s+=\s+0\)}; $in_section=0, next if m{^\s*end\s+if;}; # next could be omitted...h +ere print if $in_section; } __END__ read this data 3; read data 4; read this data 6;
    This one reads input from a list of files given by argument (@ARGV) when called as e.g. perl cond2reader.pl *.doc (or STDIN when no arguments were given). Alternatively, you can introduce an outer loop that opens all interesting files one after another (e.g. open my $fh, '<', $file or die "..."; foreach (<$fh>) { ... }
    You might need to fine-tune the regular expressions a little bit.