I want to combine this two functions together to get Shannon Diversity Index.
How can do ?
The first function is using Data::Dumper to get the unique numbers.
#!perl
use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys=1;
my @names = qw(A A A A B B B C D);
my %counts;
$counts{$_}++ for @names;
printf "\$VAR1 = { %s};\n",
join ' ',
map "$_ ",
sort { $b <=> $a }
values(%counts);
exit;
This is the output
$VAR1 = { 4 3 1 1 };
Then I can input it into the second function.
The second function is using Statistics::Diversity::Shannon
to get Shannon Diversity Index.
#!perl
use warnings;
use strict;
use Statistics::Diversity::Shannon;
my @data = qw( 4 3 1 1 );
my $d = Statistics::Diversity::Shannon->new( data => \@data );
my $H = $d->index();
my $E = $d->evenness();
print "$d/$H/$E";
exit;
How can I combine this two functions into a whole loop by using the original data set (A A A A B B B C D) to get the Shannon Diversity Index.