in reply to tracking words in lines
Try this:
use strict; use warnings; while ( my $line = <DATA> ) { next unless $line =~ /\band\b/; chomp $line; my @words = split /\s+/, $line; my $location = 0; my @locations; foreach ( @words ) { $location++; push @locations, $location if m/\band\b/; } local $" = ", "; print "Line $.: 'and' appears as word @locations.\n"; } __DATA__ This is a test and a test and a test.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: tracking words in lines
by sulfericacid (Deacon) on Nov 12, 2004 at 19:22 UTC | |
by davido (Cardinal) on Nov 12, 2004 at 19:38 UTC | |
by sulfericacid (Deacon) on Nov 12, 2004 at 19:46 UTC |