Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Block/array manipulation

From: chris:ross-gill at: 15-Oct-2001 15:19

Hi Mat,
> Here's a simple thing which I've needed to do quite a bit of, but > couldn't quite figure out how you'd do it in Rebol so tended to do > things the long way. > > Let's take a block of stuff; > > Stuff: make block! ["fire" "water" "air" "water" "water" "air"] > > Now the object of the excersize is to to build block with all of the > unique values. That bit is easy; > > foundstuff: make block! [] > foreach bitofstuff stuff [ > if found? find foundstuff bitofstuff [ > append foundstuff bitofstuff > ] > ] > > After this, all of the unique values are loaded into the 'foundstuff' > block. > > The question is, say I wanted to do this but count each instance of > the words as they are subsequently found? IE I could somehow tell that > water was found 3 times and air was found twice.
I think this function addresses your needs, but I'm sure it can be further refined... usage - unique-stuff: block-stats stuff - Chris -- REBOL [] block-stats: func [ in-blk [block!] /local count mark work-blk out-blk ][ set 'count 0 out-blk: copy work-blk: union copy in-blk [] foreach item work-blk [ while [mark: find in-blk item] [ count: count + 1 in-blk: next mark ] in-blk: head in-blk insert next find out-blk item count set 'count 0 ] return out-blk ]