# calculate an average - PERL Miscellaneous

Jack wrote:    How 'bout something like: foreach my \$idx (0 .. \$#data-2) { print "average=", (\$data[\$idx] + \$data[\$idx+1] + \$data[\$idx+2])/3, "\n"; } You'll need to deal with rounding or truncating...but that's covered in the perl FAQs....

1. ## Re: calculate an average

Jack wrote:

How 'bout something like:

foreach my \$idx (0 .. \$#data-2)
{
print "average=",
(\$data[\$idx] + \$data[\$idx+1] + \$data[\$idx+2])/3, "\n";
}

You'll need to deal with rounding or truncating...but that's covered in
the perl FAQs.

J. Guest

2. ## Re: calculate an average

Jack wrote:

<code snipped>

You'd be able to shorten the code by using the splice() function:

foreach my \$item ( splice data, -3 )

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Gunnar Guest

3. ## Re: calculate an average

In article <pxUnb.35\$uswest.net>,
"J. Gleixner" <invalid> wrote:

>
>
> How 'bout something like:
>
> foreach my \$idx (0 .. \$#data-2)
> {
> print "average=",
> (\$data[\$idx] + \$data[\$idx+1] + \$data[\$idx+2])/3, "\n";
> }
>
> You'll need to deal with rounding or truncating...but that's covered in
> the perl FAQs.
>[/ref]

not quite. with this data:

my data = qw/1 2 3 4 5 6 7/;

that code produces these results:

average=2
average=3
average=4
average=5
average=6

my \$size = 3;
my \$idx = 0;
while (1) {
print "average=",
(\$data[\$idx] + \$data[\$idx+1] + \$data[\$idx+2])/\$size, "\n";
\$idx += \$size;
last if \$idx > \$#data;
}

this code produces:

average=2
average=5
average=2.33333333333333

hth-
--
Michael Budash
Michael Guest

4. ## Re: calculate an average

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, 30 Oct 2003 05:57:32 -0800, Jack wrote:

[snip...]

Try this:

# Assumes that scalar(data) >= 3
my data = 1..10;
my \$avg = \$data[0];
foreach (1..\$#data) {
\$avg += \$data[\$_];
\$avg -= \$data[\$_-3] if(\$_>2);
print \$avg/3 if (\$_ >= 2);
}
__END__

- Brian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/oBpmiK/rA3tCpFYRAnBoAJ9aydbV4k7XXeabELeGSHW1x8A/mQCgksuC
XNYn5odaocxpgZMNfAv4NHs=
=EevM
-----END PGP SIGNATURE-----

Brian Guest

5. ## Re: calculate an average

Michael Budash wrote:

I took "every last-3-values" as [0-2], [1-3], ... instead of [0-2],
[3-5], ... oh well, one of them likely does what the OP wants.

See ya

J. Guest

6. ## Re: calculate an average

In article <vTVnb.50\$uswest.net>,
"J. Gleixner" <invalid> wrote:

>
> I took "every last-3-values" as [0-2], [1-3], ... instead of [0-2],
> [3-5], ... oh well, one of them likely does what the OP wants.
>
> See ya
>[/ref]

his original code (however obfuscated) seemed to indicate he wanted the
latter, but, as you say, "one of them likely does what the OP wants."

cheers

--
Michael Budash
Michael Guest

7. ## Re: calculate an average

"Jack" <com.> wrote:

: I need to calculate an average of every last-3-values.
:
: Foreach my \$item ( data ) {
^
^ Syntax error

: If ( \$#A == 2) {
^
^ Another syntax error

^
^ Should this be a zero?

: shift A;
: push A, \$item
^
^ Missing semicolon

: foreach my \$j ( A ) { \$add += \$j };
: } else {
: push A, \$item;
: if ( \$#A == 2 ) {
: foreach my \$j ( A ) { \$add += \$j };
: }
: }

Missing a right curly bracket here.

: I feel there is a better way or faster code than this

One without as many errors, surely. :)

There is way too much duplicated code. Rewriting it into a work-alike
might look like:

my A;
foreach my \$item ( data ) {
push A, \$item;
next if A < 3;
shift A if A > 3;
print "average: \$av\n";
}

Array slicing would be more convenient than pushing/shifting values in a
separate array. Using the sum() routine from the List::Util module
eliminates even more clutter.

use List::Util 'sum';
for(0 .. \$#data-2) {
my \$av = sum( data[\$_ .. \$_+2] ) / 3;
print "average: \$av\n";
}

Jay Guest

8. ## calculate an average

I need to calculate an average of every last-3-values.

Foreach my \$item ( data ) {

If ( \$#A == 2) {

shift A;

push A, \$item

foreach my \$j ( A ) { \$add += \$j };

} else {

push A, \$item;

if ( \$#A == 2 ) {

foreach my \$j ( A ) { \$add += \$j };

}

}

I feel there is a better way or faster code than this

Jack Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•