keen_novice has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks, My intention is to count the number of words in List 2, that contain each of the letters in List 1. When i run the code the first count is fine, however, the subsequent counts are added to the previous ones, such that the final count is the sum of all the counts, not the count of how many "words" contain an "F", as i want it to be. Where am i going wrong? Here is my code.
use warnings; use strict; my $count=0; my @list1 = ("A", "B", "C", "D", "E", "F"); my @list2 = ("AXE", "DOG", "CAT", "FOOD", "TRANCE"); for (my $i=0; $i<scalar(@list1); $i++){ for (my $j=0; $j<scalar(@list2); $j++){ my $word = $list2[$j]; my $letter = $list1[$i]; if ($word =~ /$letter/){ $count++; } } print "$count \n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Nested loops; Evaluations array elements are done repeatedly (cross-post)
by toolic (Bishop) on Nov 19, 2013 at 00:50 UTC | |
|
Re: Nested loops; Evaluations array elements are done repeatedly
by BrowserUk (Patriarch) on Nov 19, 2013 at 00:47 UTC | |
|
Re: Nested loops; Evaluations array elements are done repeatedly
by AnomalousMonk (Archbishop) on Nov 19, 2013 at 18:32 UTC | |
|
Re: Nested loops; Evaluations array elements are done repeatedly
by Laurent_R (Canon) on Nov 19, 2013 at 07:13 UTC |