4

For example

array (
product1_quantity => 5,
product1_quantity => 1,
product2_quantity => 3,
product2_quantity => 7,
product3_quantity => 2,
)

with result:

product1_quantity - 6, 
product2_quantity - 10, 
product3_quantity - 2

Thanx!


sorry, guys

stupid example, instead this really

Array ( [0] => Array ( [product1] => 7 ) [1] => Array ( [product1] => 2 ) [2] => Array ( [product2] => 3 ) )

?

6
  • 2
    Uhm language? Normally you can have only one value per key. So the array you provide is not even valid. Commented Mar 24, 2010 at 16:11
  • I don't know any language in which it is possible to have the same key for different values. Commented Mar 24, 2010 at 16:12
  • @felix, neo - those might be just some tuples Commented Mar 24, 2010 at 16:13
  • Let me see if I understand now. You want an array each element of which is an array each element of which is a key/value pair? Or is each element of the inner array a set of key/value pairs? Also: What are you trying to accomplish with this? Commented Mar 24, 2010 at 16:36
  • Perl allows for easy interchangeability between arrays and hashes, as hashes evaluates in the whole are reported as arrays where every two items makes a key/value pair. Thus, you can do stuff like: my @array = ( this => 'that', foo => 'bar', this => 'other' ); my %hash = @array; # Two keys, "this" gets overwritten. Commented Mar 24, 2010 at 19:10

4 Answers 4

3

Pull items off two at a time, and add to hash.

my @array = (
        product1_quantity => 5,
        product1_quantity => 1,
        product2_quantity => 3,
        product2_quantity => 7,
        product3_quantity => 2,
);
my %sums;
while (@array and my ($k,$v) = (shift(@array),shift(@array)) ) {
        $sums{$k} += $v;
}
Sign up to request clarification or add additional context in comments.

1 Comment

whoops, missed that this is basically IDENTICAL to other answer, who beat me by two hours. Sorry!
2

You'd want something similar to:

  use Data::Dumper;
  my @input = ( product1_quantity => 5,
                product1_quantity => 1,
                product2_quantity => 3,
                product2_quantity => 7,
                product3_quantity => 2,
              );
  my %output;

  while (my $product = shift(@input) and my $quantity = shift(@input)) {
    $output{$product} += $quantity;
  }

  print Dumper %output;

This spits out:

$VAR1 = 'product2_quantity';
$VAR2 = 10;
$VAR3 = 'product3_quantity';
$VAR4 = 2;
$VAR5 = 'product1_quantity';
$VAR6 = 6;

Be warned though -- if you've got any undefs in your quantity values this is going to break hard. You need to have an even numbered length array of product/numeric quantity pairs.

Comments

0
new_array;
foreach p in array:
  if(new_array.contains(p.key))
    new_array[p.key] += p.value;
  else
    new_array[p.key] = p.value;

new_array will contain the sums

1 Comment

Perl wasn't mentioned in the original post when I wrote this - see the revision history for the post.. "thanks" for -1
0

In Perl:

my %hash = ();
$hash{'product1_quantity'} += 5;
$hash{'product1_quantity'} += 1;
$hash{'product2_quantity'} += 3;
$hash{'product2_quantity'} += 7;
$hash{'product3_quantity'} += 2;

say join ",\n", map { "$_ - $hash{$_}" } keys %hash;

Output is:

product2_quantity - 10,
product3_quantity - 2,
product1_quantity - 6

The order is different, but you could force it to be "in order" by adding sorting:

say join ",\n", map { "$_ - $hash{$_}" } sort {$a cmp $b} keys %hash;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.