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

[REBOL] Re: Newbie Q: Search and delete from a Block

From: jason:cunliffe:verizon at: 3-Oct-2002 17:02

> How do I go about searching and deleting from a block? > > Ex. > ["hello" "there" "this" "is" "an" "Example"] > > I want to search for "this" and delete it to result with: > ["hello" "there" "is" "an" "Example"]
Hi Matt It's a good question and great intro to Rebol. There are probably 1001 cool way to do this in Rebol. The magic words are 'find and 'remove. Try this at the shell [rebol console]
>> thread: ["hello" "there" "this" "is" "an" "Example"]
== ["hello" "there" "this" "is" "an" "Example"]
>> remove find thread "this"
== ["is" "an" "Example"]
>> head thread
== ["hello" "there" "is" "an" "Example"] Voila! So what happened? Let's start again...
>> thread: ["hello" "there" "this" "is" "an" "Example"]
== ["hello" "there" "this" "is" "an" "Example"]
>> find thread "this"
== ["this" "is" "an" "Example"] See 'find has located the first instance of "this" in the block named 'thread'
>> index? find thread "this"
== 3 this is the 3rd item in block so where we now?
>> index? thread
== 1 we are at the beginning of the block
>> ? remove
USAGE: REMOVE series /part range DESCRIPTION: Removes value(s) from a series and returns after the remove. REMOVE is an action value. ARGUMENTS: series -- (Type: series port bitset none) REFINEMENTS: /part -- Removes to a given length or position. range -- (Type: number series port) [REBOL = Regular Expression Based Object Language] Rebol lets us pass expressions to each other. That's why we can use: find thread "this" as an input argument to the 'remove action word
>> remove find thread "this"
== ["is" "an" "Example"] Another way is to use 'at
>> ? at
USAGE: AT series index DESCRIPTION: Returns the series at the specified index. AT is an action value. ARGUMENTS: series -- (Type: series) index -- Can be positive, negative, or zero. (Type: number) Let's start once again
>> thread: ["hello" "there" "this" "is" "an" "Example"]
== ["hello" "there" "this" "is" "an" "Example"]
>> remove at thread 3
== ["is" "an" "Example"]
>> head thread
== ["hello" "there" "is" "an" "Example"] We could also replace the 3 above with a richer expression. Once more.. == ["hello" "there" "is" "an" "Example"]
>> thread: ["hello" "there" "this" "is" "an" "Example"]
== ["hello" "there" "this" "is" "an" "Example"]
>> remove at thread index? find thread "this"
== ["is" "an" "Example"]
>> head thread
== ["hello" "there" "is" "an" "Example"] Play around with this as much as possible at the beginning, and any time your are not sure, just go back the basics. The first couple of chapters in Rebol about series and blocks are full of magic, including some ideas which are quite different from most other languages. I do not recommend programming Rebol too much with compounded one-line phrases like remove at thread index? find thread "this" except for fun and insight in a personal sessions at the shell. Too many of them soon becomes hard/impossible to read. But it is very important to develop a fluency in the basic idioms. I find it is it is better to cut things up into many clean phrases with well-named words, then to pass those along instead. When you then later those words into more versatile functions your code will keep working and growing quickly. I am sure others here have better explanations than mine. Hope this helps anyway. ./Jason