Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
While I'm reading Learning Perl, I stumble across exercise #2 in Chapter 9. The answer goes like this:
This is supposed to pull out and print all lines that start with =item and are followed by a Perl identifier name. So using this, I wrote something that should do something similar.@ARGV = '/path/to/perlfunc.pod'; while(<>) { if (/^=item\s+([a-z_]\w*)/i) { print "$1\n" } }
When I put terms into @inputs using @ARGV, this only prints the first match it finds, even though there are more, also additional $term values don't get matched. I know this can be done using grep, but I wanted to write it in perl.sub findtext { @filenumbers = @_; foreach $number(@filenumbers) { push @filenumbers2, split(/\W/, $number); } foreach $number(@numbers2) { chomp $number; if( defined $number ) { open(FILE,"/home/jroberts/$number.txt") or die "$!"; foreach $term(@inputs) { while(<FILE>) if(/\b($term)\b/i) { push @before, split(' ', $`); @before = reverse(@before); @before = splice(@before, 0, 7); @before = reverse(@before); push @after, split(' ', $'); @after = splice(@after, 0, 7); if(exists $results{$number}) { $existing = $results{$number}; $results{$number} = $existing . "... @before" . "<b>$&</b> +" . "@after ..."; } else { $results{$number} = "... @before" . "<b>$&</b>" . "@after +"; } @before = undef; @after = undef; next; } else { print "No Match\n"; next; } } print "Match found in $number.txt\n"; @fulltext = $results{$number}; print "@fulltext\n"; close(FILE); } else { next; } next; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pattern Matching With Regular Expressions
by Paladin (Vicar) on Apr 13, 2004 at 03:37 UTC | |
by Anonymous Monk on Apr 13, 2004 at 03:47 UTC | |
by Thelonius (Priest) on Apr 13, 2004 at 04:16 UTC | |
by graff (Chancellor) on Apr 13, 2004 at 04:13 UTC | |
|
Re: Pattern Matching With Regular Expressions
by graff (Chancellor) on Apr 13, 2004 at 04:08 UTC | |
by Anonymous Monk on Apr 13, 2004 at 04:37 UTC | |
by graff (Chancellor) on Apr 13, 2004 at 04:48 UTC | |
by Anonymous Monk on Apr 13, 2004 at 05:01 UTC | |
by graff (Chancellor) on Apr 13, 2004 at 05:55 UTC | |
|