1

I have an array that is like

my @array = ('cookies','balls','cookies','balls','balls');

but the real one is much bigger/longer.

How do I output the count of each repeated string in the array?

like in the example, cookies is 2 and balls is 3.

0

4 Answers 4

7

I think we can use

map {$count{$_}++;} @array;

instead of

foreach(@array)
{
    unless(defined($count{$_}))
    {
        $count{$_} = 1;
    }
    else {
        $count{$_}++;
    }
}

to simplify the code.

Sign up to request clarification or add additional context in comments.

2 Comments

Using map in a void context is a bad idea. I'd write that as $count{$_}++ foreach @array.
You don't have to check if a key is defined before you use the increment operator on it, it will automatically inc undef to 1, without warning. You can just do $count{$_}++ for @array.
5

"How do I output the count of each repeated string in the array?"

#!/usr/bin/perl

use strict;
use warnings;

my @array = ('cookies','balls','cookies','balls','balls', 'orphan');

my %count;
$count{$_}++ foreach @array;

#removing the lonely strings
while (my ($key, $value) = each(%count)) {
    if ($value == 1) {
        delete($count{$key});
    }
}

#output the counts
while (my ($key, $value) = each(%count)) {
    print "$key:$value\n";
}

Prints:

cookies:2
balls:3

Mind, that 'orphan' wasn't output.

12 Comments

is there a way to print in the format cookies:2 /n balls:3?
@user2285115 sure, will do it now
what do you mean by 'orphan'?
@user2285115 "what do you mean by 'orphan'?" - you said, that only repeated strings should be counted. So I decided to create a non-repeated string in array to make sure, that the algorithm works correctly.
"Do you know, why Perl doesn't generate a warning, when you refer to an undefined hash element" - yes, it's called 'autovivification' and it's one of Perl's best features.
|
4

Using Perl that's a little more idiomatic than some of the other answers...

use strict;
use warnings;
use 5.010;

my @array = ('cookies','balls','cookies','balls','balls');

my %count;
$count{$_}++ foreach @array;

say "$_: $count{$_}" foreach grep { $count{$_} != 1 } keys %count;

Comments

1

Try this more shorter code u will not get any thing shorter than this

my @array = ('cookies','balls','cookies','balls','balls');
my $hashh = {};
foreach (@array){
       if(exists $hashh->{$_}){
               $hashh->{$_}++;
       } else{
               $hashh->{$_} = 1;
       }
}
print Dumper($hashh);

2 Comments

Removing the if ... else would be shorter. If $hashh->{$_} doesn't exist, it'll automatically be created when you increment it. $hashh->{$_}++ for @array;
I think that $hashh->{$_}++ for @array is quite a lot shorter than your foreach loop :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.