Hi Michael

The UNIQUE function in REBOL is handy for this sort of thing. Here is my
solution to your question:

count: func [x /local count][
 foreach ux unique x [
  count: 0
  foreach item x [
   if item = ux [count: count + 1]
  ]
  print [ux count]
 ]
]

Example:

>> str: "sdfsdfsdfsdfsdfsd"
== "sdfsdfsdfsdfsdfsd"
>> count str
s 6
d 6
f 5

Perhaps others can find a more elegant solution?

Cheers
-Larry

----- Original Message -----
From: Michael Berrisford <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, November 15, 2000 4:19 PM
Subject: [REBOL] Simple things should be simple to do ?


> REBOL/View 0.10.38.3.1 2-Oct-2000
> Copyright 2000 REBOL Technologies.  All rights reserved.
> >> str: "sdfsdfsdfsdfsdfsd"
> == "sdfsdfsdfsdfsdfsd"
> >> counts: copy []
> == []
> >> foreach char str [either none? find counts char [append counts
> reduce [char 1]] [
> [        x: (index? find counts char) + 1
> [        y: (pick counts x) + 1
> [        poke counts x y]]
> == [#"s" 6 #"d" 6 #"f" 5]
> >>
> >> foreach [a b] counts [print [a " " b]]
> s   6
> d   6
> f   5
>
>
> Hello All,
>
> The code above prints out for each unique character in str, the number
> of times it occurs. Very simple and works just fine, but it isn't very
> Rebolly. I had to do the Find twice and i don't like all the code
> involved in adding 1 to the count.
>
> Any suggestions to improve it?
>
> TIA Mike.
>
> --
> To unsubscribe from this list, please send an email to
> [EMAIL PROTECTED] with "unsubscribe" in the
> subject, without the quotes.
>

-- 
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the 
subject, without the quotes.

Reply via email to