Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Help on array element counting

by TomDLux (Vicar)
on Oct 27, 2003 at 19:31 UTC ( [id://302485]=note: print w/replies, xml ) Need Help??


in reply to Help on array element counting

my %fruits; $fruits{$_}++ for ( @fruits ); my ( $fruit, $count ); for ( $fruit, $count ) ( each %fruits ) { print "$fruit => $count\n"; }

--
TTTATCGGTCGTTATATAGATGTTTGCA

Replies are listed 'Best First'.
Re: Re: Help on array element counting
by Anonymous Monk on Oct 27, 2003 at 19:35 UTC
    if you don't mind can u explain me what is going on here .. i would like to learn it as well so i understand it alot better thanks again
      # Declare a hash named fruits. A hash contains # { key,value } pairs where the key fields are unique. my %fruits; # Get each element in the @fruits array and assign it to #the { key } portion of the hash. Also, increment the #{ value } part of the hash (the value starts with 0). #This will count the number of times the { key } appears in #the array $fruits{$_}++ for ( @fruits ); #Declare variables for { key, value } my ( $fruit, $count ); #Get each { key, value } pair and assign them to #{ fruit, count } for ( $fruit, $count ) ( each %fruits ) { #Print out the number of times the fruit existed. print "$fruit => $count\n"; }

        This is a more 'perl baby-talk' version of monktim's code, in which he exploits several perl idioms which you may not be familiar with (yet!):

        my %fruit_counts; foreach my $fruit(@fruit_list) { $fruit_counts{$fruit} = $fruit_counts{$fruit} + 1; #lhand for '++' } foreach my $fruit(keys %fruit_counts) { print "$fruit => ", $fruit_counts{$fruit}, "\n"; }

        This is one of the neatest things about Perl! monktim's code and mine look very different but are very similar in an elemental sense.

      The idea is to loop through the original array. For each element, either look up or add that element as a key into a hash, and increments that key's value by one. Since Perl's hashes use autovivification, when we try to look up a non-existing key in a hash, that key will be added.

      $hash{$_}++ for @array;
      is a very common Perl idiom. If we are processing the element 'APPLE' and it is not in the hash, a new key will be added to the hash, and that key's value (currently undef) will be incremented by 1 ... yielding 1. Later, if we encounter 'APPLE' again, when we try to look up 'APPLE' it exists this time and the value returned is 1. Add 1 to that and now we have 2 apples.

      Remember, Data::Dumper is your friend! Try this out:
      use strict; use warnings; use Data::Dumper; my @fruit = qw( MANGO APPLE GRAPES MANGO MANGO MANGO MANGO APPLES APPLES BANANA CORN APPLES ); my %fruit; $fruit{$_}++ for @fruit; print Dumper \@fruit; print Dumper \%fruit;

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://302485]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found