> From: PackRat
> I have a table where each row has a key and a value. The rows are in
> sorted order by key, and each key occurs one or more times. My goal is
> to create a new table where each key appears only once, and the values
> for each key are summed into a single value associated with each key.
> Here's an example:
>
> Starting condition: End result:
> (t1) (t2)
>
> +-----+--+ +-----+--+
> |alpha|3 | |alpha|24|
> +-----+--+ +-----+--+
> |alpha|14| |beta |73|
> +-----+--+ +-----+--+
> |alpha|7 | |delta|75|
> +-----+--+ +-----+--+
> |beta |73| |gamma|22|
> +-----+--+ +-----+--+
> |delta|21| |omega|37|
> +-----+--+ +-----+--+
> |delta|15|
> +-----+--+
> |delta|6 |
> +-----+--+
> |delta|33|
> +-----+--+
> |gamma|5 |
> +-----+--+
> |gamma|17|
> +-----+--+
> |omega|37|
> +-----+--+
Both Raul and Bill have given you good tacit answers.
In case it helps here is an explicit version:
NB. box the 2nd column according to the keys in the first column
({."1 t1) </. {:"1 t1
+--------+----+------------+------+----+
|+-+--+-+|+--+|+--+--+-+--+|+-+--+|+--+|
||3|14|7|||73|||21|15|6|33|||5|17|||37||
|+-+--+-+|+--+|+--+--+-+--+|+-+--+|+--+|
+--------+----+------------+------+----+
NB. unbox numbers and sum inside the boxes.
+/@:>each ({."1 t1) </. {:"1 t1
+--+--+--+--+--+
|24|73|75|22|37|
+--+--+--+--+--+
NB. alternative more direct approach
({."1 t1) +each//. {:"1 t1
+--+--+--+--+--+
|24|73|75|22|37|
+--+--+--+--+--+
NB. stitch on nub of first column
(~.{."1 t1) ,. ({."1 t1) +each//. {:"1 t1
+-----+--+
|alpha|24|
+-----+--+
|beta |73|
+-----+--+
|delta|75|
+-----+--+
|gamma|22|
+-----+--+
|omega|37|
+-----+--+
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm