use associative array to calculate average & SD

E

ela

In a for loop retreiving individual $numAttr to derive the $tnumAttr, the
following will serve:

for ($i=0; $i<3; $i++) {
....
$tnumAttr += $numAttr;
....
}

However, I still have the following variables or more to get the totals,

$mcc,$se,$ppv,$sp,$npv,$tp,$tn,$fp,$fn

how to use the concept of associative array to get something like:

array->[mcc], array->[se] etc. so I can write a function and just pass the
arguments like:

Sum ($mcc,$se,$ppv,$sp,$npv,$tp,$tn,$fp,$fn);



sub Sum {

foreach $_ (@_) {

$array->[$_] += $_;

}

}
 
D

derykus

In a for loop retreiving individual $numAttr to derive the $tnumAttr, the
following will serve:

for ($i=0; $i<3; $i++) {
...
   $tnumAttr += $numAttr;
...

}

However, I still have the following variables or more to get the totals,

$mcc,$se,$ppv,$sp,$npv,$tp,$tn,$fp,$fn

how to use the concept of associative array to get something like:

array->[mcc], array->[se] etc. so I can write a function and just pass the
arguments like:

Sum ($mcc,$se,$ppv,$sp,$npv,$tp,$tn,$fp,$fn);

sub Sum {

   foreach $_ (@_) {

       $array->[$_] += $_;

   }

}


http://search.cpan.org/~colink/Statistics-Descriptive-2.6/Descriptive.pm
 
D

derykus

http://search.cpan.org/~colink/Statistics-Descriptive-2.6/Descriptive.pm

--
Charles DeRykus

It's a link talking about stat but I'm asking array of array.

each $mcc, $tp, $tn will have to be calculated for its own stat. I wish to
handle them generically.

Have you looked at the docs, eg. perldoc perldsc,
perldoc perllol, etc? This should help jumpstart
some code you can write on your own. Post your
code here if you run into snags or want alternative
solutions.
 
E

ela

Have you looked at the docs, eg. perldoc perldsc,
perldoc perllol, etc? This should help jumpstart
some code you can write on your own. Post your
code here if you run into snags or want alternative
solutions.

--
Charles DeRykus

Thanks for your reference again. But neither of them talks about how to
create variables dynamically. Let me illustrate:

$var1, $var2, ..., $varn

$array[var1] #impossible because var1 is a string

or $array->[var1] #possible but in fact previously it's $var1 but not var1

what i have to get is to somehow "de-reference" (I wish this's the correct
term) the variables 1 to n to get their names and then use these names to
hash the associative arrays.
 
T

Tad J McClellan

[ Please learn the proper way of composing followup messages.

Have you seen the Posting Guidelines that are posted here frequently?
]




There should be an attribution line here stating who is being quoted.

(like I have above, indicating that I am quoting something
that "ela" said.)

Have you looked at the docs, eg. perldoc perldsc,
perldoc perllol, etc? This should help jumpstart
some code you can write on your own. Post your
code here if you run into snags or want alternative
solutions.


There should be some sort of quoting character here so that readers
can tell the difference between text that is quoted and text that
is new.

(like the ">" above, indicating that I am quoting those 5 lines.



You should not quote .sigs.

Thanks for your reference again. But neither of them talks about how to
create variables dynamically.


You should stop wanting to create variables
dynamically (ie. symbolic references).

You can accomplish what you want without it.

Let me illustrate:

$var1, $var2, ..., $varn


Instead of having N separate variables, have a single hash
(associative array) with N keys:

$hash{var1} = calc_var1();
$hash{var2} = calc_var2();
...
$hash{varn} = calc_varn();

$array[var1] #impossible because var1 is a string


Hashes _are_ indexed by strings.

So if you want something that is indexed by strings, you should be
using a hash. Then

$hash{var1}

will work just fine.

or $array->[var1] #possible but in fact previously it's $var1 but not var1

what i have to get is to somehow "de-reference" (I wish this's the correct
term)


There are no references in anything I've suggested above, just
plain old hash data structures.
 
E

ela

Tad J McClellan said:
Instead of having N separate variables, have a single hash
(associative array) with N keys:

$hash{var1} = calc_var1();
$hash{var2} = calc_var2();
...
$hash{varn} = calc_varn();

In this sense, I can't create a function to handle the problem generically.
In other words, every time I have to write the codes (or lines) explicitly.
Although I have to declare "n" variables, it's not good to do the "n" things
again & again (e.g. take average, calculate SD and whatever downstream
operations). I wish to use for loop with a function to achieve this.
 
T

Tad J McClellan

You have provided an attribution and quoting characters in your followup.

Thank you.



But you are still quoting .sigs.

Please stop doing that.

In this sense, I can't create a function to handle the problem generically.


Probably because we cannot divine what "the problem" is.

If you post a short and complete program *that we can run* that
illustrates the problem, then we can almost for sure provide
a solution...

In other words, every time I have to write the codes (or lines) explicitly.
Although I have to declare "n" variables, it's not good to do the "n" things
again & again (e.g. take average, calculate SD and whatever downstream
operations). I wish to use for loop with a function to achieve this.


Now I am begining to think that you are looking for a "dispatch table".

Let us fix N at 2 (avg and SD) so we have something concrete to work with.

# UNTESTED
my %dispatch = (
avg => \&compute_average,
sd => \&compute_sd,
);

my @results; # LoH, 1 array element for each data set,
# each hash has 2 keys: "avg" and "sd"

foreach my $dataset ( @data ) {
my %h; # holds all statistics for one data set

foreach my $statistic ( keys %dispatch ) {
$h{$statistic} = $dispatch{$statistic}->($dataset);
}

push @results, \%h;
}

foreach my $result ( @results ) {
display_result($result);
}

If you then need to change N to 3, you'll need to add only one line
to the dispatch table:

median => \&calculate_median,
 
D

derykus

...

Thanks for your reference again. But neither of them talks about how to
create variables dynamically. Let me illustrate:

$var1, $var2, ..., $varn

$array[var1] #impossible because var1 is a string

or $array->[var1] #possible but in fact previously it's $var1 but not var1

what i have to get is to somehow "de-reference" (I wish this's the correct
term) the variables 1 to n to get their names and then use these names to
hash the associative arrays.

Here's some background about why it's usually a bad
idea to use that approach:

perldoc -q "variable as a variable name"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top