Hi Chris, On Sun, 19 Aug 2012 08:49:25 -0500 Chris Stinemetz <perlqu...@gmail.com> wrote:
> Hello List, > > I have input data such as far below: > > I would like to read the data into an array and modify the 2nd index > if the 0th and first indices are identical. > I would like the updated 2nd index to be an average of the 2nd index > where both occurences of 0th and 1st indices match. > > So for example below: > > The records that contain 24 1 I would like to average the second > index 60 and 40 I would suggest two passes. In the first pass you maintain a hash that maps the tuple of the 0th and 1st columns into a count-and-sum record: # Untested my %counts_and_sums; foreach my $item (@array_of_arrays) { my $key = join("\0", $item->[0], $item->[1]); # Or some other suitable serialisation method. $counts_and_sums{$key}{count}++; $counts_and_sum{$key}{sum} += $item->[2]; } Then in the second path, you look up the items' keys in the hash, and if ->{count} is greater than 1 you assign ->{sum} / ->{count} to $item->[2]. Regards, Shlomi Fish > > and print the new value like this: > > ... # print input data > 24 1 50 65 > 24 1 50 65 > ... # print rest of input data > > > > > 22 1 60 65 > 22 2 180 90 > 22 3 300 90 > 23 1 0 65 > 23 2 90 65 > 23 3 260 65 > 24 1 60 65 > 24 1 40 65 > 24 2 180 65 > 24 2 160 65 > 24 3 310 65 > 24 3 290 65 > 25 1 0 90 > 25 2 120 65 > 25 3 240 65 > 26 1 60 65 > 26 2 180 65 > 26 3 320 65 > 87 1 355 65 > 87 2 100 65 > 87 3 245 65 > 87 3 180 65 > 88 1 100 65 > 88 2 210 65 > 88 3 310 65 > > Thank you in advance. -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/ Logic sucks. Morality sucks. Reality sucks. Deal with it! Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/