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.
Data::Dumper is a debugging tool, not a serializing too. Not a good one, at least.
But you aren't even using Data::Dumper. You're using something far worse.
Let's start by using something acceptable like JSON.
(Note that the
sort { $b <=> $a }doesn't appear required.)And this is one way to read it back in:
Above, I assumed you meant "work together" when you said "combine into whole loop".
On the other hand, maybe you meant "combine into a single file". If that's the case, then you can use the following: