perldata-dumper

How can I combine Data::Dumper and Statistics::Diversity::Shannon into a whole loop?


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.


Solution

  • 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.

    #!/usr/bin/perl 
    
    use strict;
    use warnings;
    
    use Cpanel::JSON::XS qw( encode_json );
    
    {
       my @names = qw( A A A A B B B C D );
    
       my %counts; ++$counts{$_} for @names;
       my @data = sort { $b <=> $a } values(%counts);
    
       print encode_json(\@data);
    }
    

    (Note that the sort { $b <=> $a } doesn't appear required.)

    And this is one way to read it back in:

    #!/usr/bin/perl 
    
    use strict;
    use warnings;
    
    use Cpanel::JSON::XS               qw( decode_json );
    use Statistics::Diversity::Shannon qw( );
    
    {
       my $json = do { local $/; <> };
       my $data = decode_json($json);
    
       my $d = Statistics::Diversity::Shannon->new( data => $data );
       my $H = $d->index();
       my $E = $d->evenness();
    
       print "$H/$E\n";
    }
    

    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:

    #!/usr/bin/perl 
    
    use strict;
    use warnings;
    
    use Statistics::Diversity::Shannon qw( );
    
    {
       my @names = qw( A A A A B B B C D );
    
       my %counts; ++$counts{$_} for @names;
       my @data = values(%counts);
    
       my $d = Statistics::Diversity::Shannon->new( data => \@data );
       my $H = $d->index();
       my $E = $d->evenness();
    
       print "$H/$E\n";
    }