Dear All,

This is my first post.

I present some subroutines that 1) create an index for one or more arrays and then 2) get summary statistics based on the index. I have 2 issues:

1. I would like to be able to create the index conditional on the values of another array, e.g., conceptually something like:

my @index = &defindex(\...@a,\...@b,\...@c)  for \...@dv >150 & dv < 800

However, I have no idea how to write the code to do this. Ideally I would be able to have several constraints (e.g., filter by reaction time, accuracy, etc). Any clues? Is there a specific keyword or topic I should search?

2. This following section is redundant across the subroutines, but I cannot figure out how put it into another subroutine and return the result in a way that I can use it. I think I'm just not understanding how to correctly return/use the hash with multiple values per key. ( I want to maintain separate subroutines for each measure)

        my @ke...@{$_[0]};
        my @valu...@{$_[1]};
        my %combos = ();        
        
        my $i=0;
        foreach (@keys) {
                my $key=$keys[$i];
                my $value=$values[$i];

                push( @{$combos{$key}}, $value );
                $i++;
        }

Finally, I have no doubt people can suggest how make the code generally more perl like and efficient.

Many thanks in advance for any help,
Eric


#!/usr/bin/perl

use strict qw(vars subs);
use warnings;
use List::Util qw(sum);

#example data
my @a = qw(Sub1 Sub1 Sub1 Sub1 Sub1 Sub1 Sub2 Sub2 Sub2 Sub2 Sub2);
my @b = qw(left left left right right left right right left right right);
my @c = qw(down up down down up up up down down up up);
my @dv= (.87,.2,.655,.7,.5,.3,.1,.45,.70,.900,.3);
my @acc= qw(c c c i c i c c c i c);


###########################################
my @index = &defindex(\...@a,\...@b,\...@c);

getmean(\...@index,\...@dv);
getmedian(\...@index,\...@dv);
getcount(\...@index,\...@dv);
getmean(\...@index,\...@acc);

#define the index
sub defindex {

        my $i;
        my $k;
        my @combined;
        my @meshed;
        
        for ( $i=0; $i<$#_+1; $i++) {
                push(@combined,${"array$i"}=$_[$i]);
        }

        @meshed = mesh(@combined);

        $j=0;
        while (@meshed){
                my $merged = join('',splice(@meshed,0,$#_+1));
                $index[$j]=$merged;
                $i++;
        }

        return @index;

} #end defindex


# get the mean by index
sub getmean (\...@\@) {
        my @ke...@{$_[0]};
        my @valu...@{$_[1]};
        my %combos = ();
        
        my $i=0;
        foreach (@keys) {
                my $key=$keys[$i];
                my $value=$values[$i];
                push( @{$combos{$key}}, $value );
                $i++;
        }
        
        print "ID\tMeans\n";
        my $key;
        my $mean;
        foreach $key (sort keys %combos) {              
                my $mean = (sum(@{$combos{$key}}))/(scalar(@{$combos{$key}}));
                printf "$key\t%.3f\n", $mean;
        }       
} #end of getmean

#get median by index
sub getmedian (\...@\@) {
        my @ke...@{$_[0]};
        my @valu...@{$_[1]};
        my %combos = ();        

        my $i=0;
        foreach (@keys) {
                my $key=$keys[$i];
                my $value=$values[$i];
                push( @{$combos{$key}}, $value );
                $i++;
        }
        
        print "ID\tMedians\n";
        my $key;
        foreach $key (sort keys %combos) {
                my $count = scalar(@{$combos{$key}});
                my @array = sort { $a <=> $b } @{$combos{$key}};
                if ($count % 2) {
                my $median= $array[int($count/2)];
                print "$key\t$median\n";

                } else {
                my $median= ($array[$count/2] + $array[$count/2 - 1]) / 2;
                print "$key\t$median\n";
                }

        }       
        
} # end of getmedian

#get count by index
sub getcount (\...@\@) {
        my @ke...@{$_[0]};
        my @valu...@{$_[1]};
        my %combos = ();        
        
        my $i=0;
        foreach (@keys) {
                my $key=$keys[$i];
                my $value=$values[$i];
                push( @{$combos{$key}}, $value );
                $i++;
        }
        
        print "ID\tCounts\n";
        my $key;
        foreach $key (sort keys %combos) {
                my $count = scalar(@{$combos{$key}});
                print "$key\t$count\n";
        }       
} # end of getcount

#borrowed from the List::MoreUtils Module by Tassilo von Parseval sub mesh (\...@\@;\...@\@\...@\@\...@\@\...@\@\...@\@\...@\@\...@\@\...@\@\...@\@\...@\@\...@\@\...@\@\...@\@\...@\@ \...@\@) {
    my $max = -1;
    $max < $#$_  &&  ($max = $#$_)  for @_;

    map { my $ix = $_; map $_->[$ix], @_; } 0..$max;
}
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to