use strict; use warnings; while ( my $line = ) { 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.