Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Re: Help on array element counting

by Anonymous Monk
on Oct 27, 2003 at 19:35 UTC ( [id://302489]=note: print w/replies, xml ) Need Help??


in reply to Re: Help on array element counting
in thread Help on array element counting

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

Replies are listed 'Best First'.
Re: Re: Re: Help on array element counting
by monktim (Friar) on Oct 27, 2003 at 19:45 UTC
    # 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.

3Re: Help on array element counting
by jeffa (Bishop) on Oct 27, 2003 at 19:58 UTC

    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://302489]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found