Google Groups Home
Help | Sign in
Better way for Grouping rows in a series.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
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
Ged Byrne  
View profile
 More options Nov 23 2006, 8:54 am
From: "Ged Byrne" <ged.by...@gmail.com>
Date: Thu, 23 Nov 2006 13:54:28 -0000
Local: Thurs, Nov 23 2006 8:54 am
Subject: Better way for Grouping rows in a series.
Hi,

After a long break I'm getting back into REBOL and I was wondering if
anybody could help me out with a few pointers on how to be more
REBOLious.

I'm using the following script to group data from a table.

It's pretty short, but I'm not happy with it.  Can anybody recommend
improvements.

Thanks.

----

rebol[]
data: [
  ;Country  Name
  "UK"      "John"
  "US"      "Bradley"
  "UK"      "George"
  "US"      "Cory"
  "US"      "Bret"
  "UK"      "Henry"
]

result: copy []

foreach country unique extract data 2 [
  append result country
  append/only result copy []
]

forskip data 2 [
  append
    select result first data
    second data
]

probe result

----

The result is this:
[
    "UK" ["John" "George" "Henry"]
    "US" ["Bradley" "Cory" "Bret"]
]


    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.
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.
Oyster  
View profile
 More options Nov 30 2006, 7:23 am
From: "Oyster" <lepto.pyt...@gmail.com>
Date: Thu, 30 Nov 2006 04:23:55 -0800
Local: Thurs, Nov 30 2006 7:23 am
Subject: Re: Better way for Grouping rows in a series.
hi, Gregg, where is the mail-list and how can I join it?
thanx

    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.
Gregg Irwin  
View profile
 More options Nov 30 2006, 10:45 am
From: Gregg Irwin <gregg.ir...@gmail.com>
Date: Thu, 30 Nov 2006 08:45:23 -0700
Local: Thurs, Nov 30 2006 10:45 am
Subject: Re: [REBOL-GG] Re: Better way for Grouping rows in a series.
Hi Oyster,

O> hi, Gregg, where is the mail-list and how can I join it?

See: http://www.rebol.com/discussion.html for details.

There is also an archive of old messages on REBOL.org.

--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.
End of messages
« Back to Discussions « Newer topic     Older topic »

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