Google Groups Home
Help | Sign in
Message from discussion Better way for Grouping rows in a series.
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Gregg Irwin  
View profile
 More options Nov 26 2006, 12:49 pm
From: Gregg Irwin <gregg.ir...@gmail.com>
Date: Sun, 26 Nov 2006 10:49:20 -0700
Local: Sun, Nov 26 2006 12:49 pm
Subject: Re: [REBOL-GG] Better way for Grouping rows in a series.
Hi Ged,

You might get a better response on the ML than here, but...

You could mod this for your needs. It expects the rows to be
sub-blocks, and it doesn't remove the column you're grouping by, but
it's a general solution. I think this was something I did early in my
REBOLing, so it could probably be greatly improved as well.

    group-by: func [  ; See SPLIT
        {Returns a block of blocks+sub-blocks with items partitioned by
        matching index elements in each sub-block.}
        block  [any-block!] "A block of series values"
        index  [integer!] "Index of sub-series value to compare, for grouping."
        /part "Compare part of the series, rather than a single value."
            range [number!]
        /local result val
    ][
        result: copy []
        either part [
            ; First, build up a list of keys, with empty blocks to go with each.
            foreach item block [
                val: copy/part at item index range
                if not find/only/skip result :val 2 [repend result [:val copy []]]
            ]
            ; Find the correct place (key) to put an item, and add it there.
            foreach item block [
                val: copy/part at item index range
                append/only select/only result :val item
            ]
        ][
            ; First, build up a list of keys, with empty blocks to go with each.
            foreach item block [
                val: item/:index
                if not find/skip result :val 2 [repend result [:val copy []]]
            ]
            ; Find the correct place (key) to put an item, and add it there.
            foreach item block [
                val: item/:index
                append/only select result val item
            ]
        ]
        result
    ]

    group-by [[a 1 2] [b 2 3] [a 2 4] [c 2 3] [b 1 5]] 1
    group-by [[a 1 2] [b 2 3] [a 2 4] [c 2 3] [b 1 5] [c 2 4]] 2
    group-by [[a 1 2] [b 2 3] [a 2 4] [c 2 3] [b 1 5] [c 2 4]] 3
    group-by/part [[a 1 2] [b 2 3] [a 2 4] [c 2 3] [b 1 5] [c 2 4]] 1 2
    group-by/part ["ABC" "AAC" "ABD"] 1 2

--Gregg


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google