Professional Web Applications Themes

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

    Default 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. #2

    Default 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 )

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

    Gunnar Guest

  3. #3

    Default 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

    try this instead:

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

    Default 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. #5

    Default 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. #6

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

    Default Re: calculate an average

    "Jack" <com.> wrote:

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

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

    : $add = o;
    ^
    ^ 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 };
    : }
    : }
    : $av = $add/3;

    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;
    my $add = 0;
    $add += $_ for A;
    my $av = $add/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. #8

    Default calculate an average

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

    my ($av, $add, A);

    Foreach my $item ( data ) {

    If ( $#A == 2) {

    $add = o;

    shift A;

    push A, $item

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

    } else {

    push A, $item;

    if ( $#A == 2 ) {

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

    }

    }

    $av = $add/3;



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



    thanks for comments


    Jack Guest

Similar Threads

  1. average days
    By JoeyTMann in forum Macromedia ColdFusion
    Replies: 4
    Last Post: April 27th, 02:40 PM
  2. average
    By Lawrence in forum FileMaker
    Replies: 4
    Last Post: February 13th, 12:36 AM
  3. Calculate Running Average
    By Arijit Chatterjee in forum IBM DB2
    Replies: 1
    Last Post: July 3rd, 04:51 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139