vindaloo has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
I am trying to perform a function on an array of arrays. It will always be an array of 2 to many arrays. They are always the same sized array, which is good as I need to sum up all of the corresponding elements. I have never needed to deal with many corresdonding arrays like this before and am stumped as to how to do this. How would you attempt to do this?
#!/usr/bin/perl use strict; use warnings; my @set = ( { 'id' => '1462', 'items' => [ '9.9', '10.1', '10.3', '10.5', '10.8', '10.94' ], }, { 'id' => '1463', 'items' => [ '3.1', '4.3', '4.5', '4.6', '4.7', '4.8' ], } ); my @aoa; foreach my $hash_ref (@set) { push @aoa, $hash_ref->{items}; } my $totals = sum_up->(\@aoa); sub sum_up { my $aoa = shift; my @totals; #\@totals would look like this: #[ # '13', # '14.4', # '14.8', # '15.1', # '15.5', # '15.74' #] return \@totals; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: performing a function on corresonding array elements
by BrowserUk (Patriarch) on Apr 03, 2006 at 01:53 UTC | |
by codeacrobat (Chaplain) on Apr 03, 2006 at 06:23 UTC | |
by BrowserUk (Patriarch) on Apr 03, 2006 at 12:39 UTC | |
by vindaloo (Acolyte) on Apr 03, 2006 at 02:29 UTC | |
|
Re: performing a function on corresonding array elements
by diotalevi (Canon) on Apr 03, 2006 at 06:48 UTC | |
by tye (Sage) on Apr 03, 2006 at 18:23 UTC | |
by diotalevi (Canon) on Apr 03, 2006 at 18:26 UTC |