On Wed, Jan 09, 2002 at 05:26:41PM +0000, beginners wrote:
1>$/=undef; # slurp mode
2>
3>for (@array_of_files) {
4>
5> my @wordfound =();
6>
7> print"***Error : Cannot find file $_\n" unless -r && -w;
8>
9> open(FILE, $_) or die "$_:$!\n";
10> my $file_contents =<FILE>; # get all the file
11>
12> @wordcount = ($file_contents =~/^\b($word)\b/g);
13> close (FILE);
14>
15> print"Word found $#wordfound times in file $_\n";
16>}
---end quoted text---
Ack, I forgot to explain that :(
1. unsets the record seperator variable (usually "\n") so there's no
need to loop for each line, smart for small files / dumb for really big
files.
3. Removing the While loop means I can still use $_, although Japhy is
right in what he says.
5. The array I use is locally scoped so is flushed every loop, I
initialise it to stop complaints if using -w.
12. using () in a regex puts the values in $1-$9 and returns each item
matched to an array (@wordcount).
15. $#wordcount returns the number of the last index in the array not
the number of elements ( so 4 elements $#wordcount = 3 IIRC ) so that
needs a check like defined $wordcount[0] to check that there is at least
one match.
It's just another way, it's not better for all instances.
--
Frank Booth - Consultant
Parasol Solutions Limited.
(www.parasolsolutions.com)
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]