alexlearn has asked for the wisdom of the Perl Monks concerning the following question:

my question is i have an array
my @findle = ("fever","febbre","la fièvre","koorts,") ; I then loop through the array and perform a regular expression for ($i=0; $i<$size; $i++) { if ($_ =~ /$findle[$i]/ ) { print "$_\n"; $number++; #@lines = # print ("there are : " . $number); } }
currently number is a scalar. What i would like todo is each time i increment through the array to the next element it will store the instances in an array element.
  • Comment on to loop through an array and store each of the subtotals in the elements of another array
  • Download Code

Replies are listed 'Best First'.
Re: to loop through an array
by kennethk (Abbot) on May 12, 2011 at 17:23 UTC
    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.

      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