Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Count how many of each type in an array

by Anonymous Monk
on Apr 04, 2003 at 18:03 UTC ( [id://248152]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Count how many of each type in an array

Replies are listed 'Best First'.
Re: arrays
by Jenda (Abbot) on Apr 04, 2003 at 18:14 UTC

    What about looping through the array with foreach(){} and incrementing either $smallboxes or $largeboxes depending on the value of the currently processed item?

    Maybe you should give us some more infromation. Is the number of possible groups of elements fixed? If it's not you may need to do something like this:

    foreach (@x) { s/\d+$//; # strip the number $counts{$_}++ } foreach my $group (sort keys %counts) { print "$group : $counts{$group} times\n"; }
    That is first you strip the numbers to get the "name" of the group and then increase the group count in the %counts hash. Then you print all the groups.

    Update: As arturo correctly points out I have a bug in the code. I'd modify the original array. The loop should look like this

    foreach (@x) { my $group = $_; $group =~ s/\d+$//; # strip the number $counts{$group}++ }
    Thanks arturo.

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      Let me add the caveat that since the

      foreach (@foo ) { }
      construction makes $_ an alias for the current element being processed, that your number stripping substitution modifies the original array, which may or may not be appropriate. To non-destructively process the array, you might capture before the first digit to get your hash key.

      If not P, what? Q maybe?
      "Sidney Morgenbesser"

Re: arrays
by dragonchild (Archbishop) on Apr 04, 2003 at 18:04 UTC
    Use grep. I'll leave how to the reader.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: arrays
by cbro (Pilgrim) on Apr 04, 2003 at 18:18 UTC
    Could you clearify this a little bit.
    Do you mean, "How do I count the number of times the elements of my array appear in a file?" If so, then I agree with dragonchild.
Re: Count how many of each type in an array
by Anonymous Monk on Apr 04, 2003 at 19:26 UTC
    my %counts; $counts{$_}++ for @x; print "$_ occured $counts{$_} times\n" for keys %counts;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-25 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found