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

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

Replies are listed 'Best First'.
Re^3: to loop through an array
by alexlearn (Initiate) on May 12, 2011 at 21:39 UTC
    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.

        I am not sure I fully understand the data that is being printed. print "$element\t=>$count{$element}\n"; I seem to getting a strange error Use of uninitialized value in concatenation (.) or string at searchfile.pl line 91, <FILE> line 1023078. Is there a problem with the hash and how its counting the elements