0

How I can sort the value of hash and count them for related key.

For example : push @{$name_hash{$st_name}},$end_name;

 $VAR1 = 'firstname')';
    $VAR2 = [
      'lastname1',
      'lastname1',
      'lastname1',
      'lastname2',
      'lastname2',
      'lastname2',
      'lastname3',
      'lastname3',
       ] 

I need table like

    `firstname : lastname1 : 3`  

    `firstname : lastname2 : 3`

    `firstname : lastname3 : 2`

I tried using some counter but its failing either at start where I am having only value multiple time or at the end of the last value (in this case lastname3).

foreach $n (sort keys %name_hash) {
    $ecounter =0; $temp2 = "NA"; $tcount =0; $p =0;
    foreach $lastn (sort values @{$name_hash{$n}}) {
        $ecounter ++ if ($lastn eq $temp2);
        $tcount++;
        if (($tcount) > ($ecounter+1) ){
        print " $n : $temp2 : $ecounter+1\n";
        $ecounter=0; $tcount=1;
        $p =1 ;
        }
        $temp2 = $lastn ;

       }

    print "$n : $temp2 : $ecounter+1\n" if (!$p);

    }
1
  • Instead of an array, why not just use another hash? Changing the push to $name_hash{$st_name}{$end_name}++ should get you what you want... Commented Apr 26, 2018 at 20:50

2 Answers 2

5

Use a hash to count the repeated names:

#!/usr/bin/perl
use warnings;
use strict;

my %name_hash = (
    firstname => [qw[
        lastname1 lastname1 lastname1
        lastname2 lastname2 lastname2
        lastname3 lastname3 ]]);

for my $first_name (sort keys %name_hash) {
    my %count;
    ++$count{$_} for @{ $name_hash{$first_name} };
    print "$first_name : $_ : $count{$_}\n" for sort keys %count;
}
Sign up to request clarification or add additional context in comments.

Comments

0
# setup data for testing
sub _GetNamesHash {
    my $names_hash = {
        'James' => [qw(Smith Jones Smith Adams Baldwin Kirk Smith)],
        'John'  => [qw(Smith Adams Smith Adams Henry Hancock)],
    };
    return $names_hash;
}

# count the last names for each first name
sub _CountNames {
    # start with a hash of first_name => arrayref of last_name
    my $names_hash = _GetNamesHash();

    # put into a hash of first_name =>
    # arrayref of hashref of last_name => count
    my $name_counts = {};
    foreach my $first_name (keys %$names_hash) {
        $name_counts->{$first_name} = {}; # hash of last_name => count
        foreach my $last_name (@{ $names_hash->{$first_name} }) {
            $name_counts->{$first_name}->{$last_name}++; # count this last_name
        }
    }

    # show counts
    foreach my $first_name (sort keys %$name_counts) {
        my $last_name_counts = $name_counts->{$first_name};
        foreach my $last_name (sort keys %$last_name_counts) {
            my $count = $last_name_counts->{$last_name};
            printf("%s : %s : %d\n", $first_name, $last_name, $count);
        }
    }

    return;
}

Comments

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.