Gary MacDonald wrote: > Someone else has noted the problem of square brackets within your > regular expressions. > > However, the basic algorithm is flawed as well. Having identified the > first token in the input stream, another record must be read before you > check for the second token. > > Here's three different ways of doing this. > > > Method One (outer loop, inner loop) > > OPEN WININFO, $winmsd; > while (<WININFO>) > { > next unless /\[System Summary\]/; > while (<WININFO>) > { > last if /\[Hardware Resources\]/; > print; > } > last; > } > CLOSE WININFO; > > > Method Two (two separate loops) > > OPEN WININFO, $winmsd; > while (<WININFO>) > { > last if /\[System Summary\]/; > } > while (<WININFO>) > { > last if /\[Hardware Resources\]/; > print; > } > CLOSE WININFO; > > > Method Three (slurp mode and regex capture) > > OPEN WININFO, $winmsd; > { > local $/; # slurp mode for this block only > $_ = <WININFO>; > ($_) = /\[System Summary\](.+?)\[Hardware Resources\]/s; > print; > } > close WININFO;
Here's two more (one uses flag, second uses .. and array [to drop last line]): open IN, $winmsd or die "Can't open '$winmsd': $!\n"; my $found; while (<IN> ) { $found++ if (not $found and /\[System Summary\]/); last if /\[Hardware Resources\]/; print if $found; } close IN; my @wanted; open IN, $winmsd or die "Can't open '$winmsd': $!\n"; while (<IN> ) { push @wanted, $_ if /\[System Summary\]/ .. /\[Hardware Resources\]/; } close IN; pop @wanted; # drop last line print @wanted; -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // http://dbecoll.tripod.com/ (Free site for Perl) -/-' /___/_<_</_</_ Castle of Medieval Myth & Magic http://www.todbe.com/ _______________________________________________ Perl-Win32-Admin mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin