# use associative array to calculate average & SD

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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->[\$_] += \$_;

}

}

--
Charles DeRykus

## Re: use associative array to calculate average & SD

--
Charles DeRykus

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

## Re: use associative array to calculate average & SD

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

## Re: use associative array to calculate average & SD

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.

## Re: use associative array to calculate average & SD

[ 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.)

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.

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

You can accomplish what you want without it.

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

\$hash = calc_var1();
\$hash = calc_var2();
...
\$hash = calc_varn();

Hashes _are_ indexed by strings.

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

\$hash

will work just fine.

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

--
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

## Re: use associative array to calculate average & SD

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.

## Re: use associative array to calculate average & SD

Thank you.

But you are still quoting .sigs.

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

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 = \$dispatch->(\$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,

--
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"