On Fri, 05 Jun 2009, PackRat wrote:
> I'm stumped again, seeking the collective wisdom. I'm sure this is a
> very common situation for those of you who work with entered data and
> thus has probably been solved already long ago:
>
> 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|
> +-----+--+
I prefer inverted table for its efficiency, Roger had written an article
on it in jwiki. Using the verb ifa (inverted from atom)
ifa
<@(>"1)@|:
ifa t1
+-----+----------------------------+
|alpha|3 14 7 73 21 15 6 33 5 17 37|
|alpha| |
|alpha| |
|beta | |
|delta| |
|delta| |
|delta| |
|delta| |
|gamma| |
|gamma| |
|omega| |
+-----+----------------------------+
(~.@>@{. ; (>@{. +//. >@{:)) ifa t1
+-----+--------------+
|alpha|24 73 75 22 37|
|beta | |
|delta| |
|gamma| |
|omega| |
+-----+--------------+
As Raul has written, /. (key) is the adverb you are looking for.
--
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
唐詩030 韋應物 長安遇馮著
客從東方來 衣上灞陵雨 問客何為來 采山因買斧
冥冥花正開 颺颺燕新乳 昨別今已春 鬢絲生幾縷
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm