in reply to tracking words in lines
#!/usr/bin/perl use warnings; use strict; my $wordcount = 0; my $linenumber = 0; my $wordnumber = 0; my $findword = "and"; open(FILE, "< input.txt") or die "Cannot open file: $!"; my @lines = <FILE>; close(FILE) or die "Cannot close file: $!"; foreach my $line (@lines) { $linenumber++; my @words = split(/\s+/, $line); foreach my $word (@words) { $wordnumber++; if ($word =~ m/^and$/) { print qq(The word "$findword" occured in line $linenumber a +t word $wordnumber\n); } } $wordnumber = 0; }
UPDATE: Fixed word splitting in regards to Enlil's message:
"but split '',$line is probably better than / /, if someone puts two spaces you might want split, / +/,$line =) "
Thanks!
|
|---|