in reply to to loop through an array and store each of the subtotals in the elements of another array

Please read Writeup Formatting Tips. In particular, please wrap code in <code> tags, to prevent mangling. In particular, note how your array indices got linkified.

After reformatting your posted code, I get something like:

#!/usr/bin/perl -w use strict; my @findle = ("fever","febbre","la fièvre","koorts"); my $size = @findle; my $number = 0; for (my $i=0; $i<$size; $i++) { if ($_ =~ /$findle[$i]/ ) { print "$_\n"; $number++; #@lines = # print ("there are : " . $number); } }

which can be written more cleanly as Your code can be written more cleanly as

#!/usr/bin/perl -w use strict; my @findle = ("fever","febbre","la fièvre","koorts"); my $number = 0; for my $element (@findle) { if (/\Q$element\E/) { print "$_\n"; $number++; #@lines = # print ("there are : " . $number); } }

If you want to cache the elements you hit on, you can combine an array outside the scope of your test with push:

#!/usr/bin/perl -w use strict; my @findle = ("fever","febbre","la fièvre","koorts"); my @hits; for my $element (@findle) { if (/\Q$element\E/) { print "$_\n"; push @hits, $element; #@lines = # print ("there are : " . $number); } }

Note that the length of your @hits array also functions to count the number of hits.

Update: OP added tags, so modified above accordingly.

Replies are listed 'Best First'.
Re^2: to loop through an array
by alexlearn (Initiate) on May 12, 2011 at 20:02 UTC
    Thanks you very much kennethk I think your method of looping through an array is better. I am having problems with understanding how the push method is supposed to work. lets say that i have found 10 occurrences of fever and then 20 occurrences of koorts I have declared an array called hit as suggested. but what I am not sure is how will 10 will be added to the first element and 20 as the second element. I am very new to Perl so am just finding my feet. I just realised that each time that each time for for loop is run it will go to the next element and then the number++ is will started again from zero to add up the occurrences of the next element in the array. i have managed to get the push array function and have performed a few example so i can familiarize myself with it. one problem when i run the print statement it only shows the total amount not the sub divided amount for each element is my test correct
      it appears that my hit array is taking the element names not the number of occurrences. I have re-arranged the code
      for my $element (@findle) { if (/\Q$element\E/ ) { print "$_\n"; $number++; # push @hit $element; push(@hit, $number); #@lines = # print ("there are : " . $number); } }
      what i am trying todo is store the number of occurences for each of the words in the findle array
        Ahh, I misunderstood your spec. Rather than just accumulating with push, you would be better served with a hash. In this case, you could accumulate frequencies with something like:

        my %count; for my $element (@findle) { if (/\Q$element\E/ ) { print "$_\n"; $number++; # push @hit $element; $count{$element}++; #@lines = # print ("there are : " . $number); } } for my $element (@findle) { print "$element\t=>$count{$element}\n"; }

        Hashes are probably Perl's greatest strength. A read through of perldata will tell you a bit more about them.