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

An object by another name

 [1/5] from: rebol665::ifrance::com at: 19-Feb-2002 21:55


Hi I've got an answer for you.
>> num: ask "Car number please?"
Car number please?3 == "3"
>> ChosenCar: get to-word join "car" num >> chosencar/model
== "Riviera"
>>
Patrick PS: I learned this method from Ingo Hohmann from the rebol-list a few month ago.

 [2/5] from: martin:middleton:speechworks at: 19-Feb-2002 14:52


Let's say I have the following: car: make object! [ make: "Ford" model: "Custom 500" color: white ] car1: make car [ model: "Escort" color: red ] car2: make car [ model: "Taurus" color: blue ] car3: make car [ make: "Buick" model: "Riviera" color: silver ] And assume that my REBOL program asks the user the to enter a number between 1 and 3 and that I save the response in something called car-choice. How can I "combine" (probably a bad choice of terminology) the value "car" and the user's response so that I end up with a value I can use to refer to the proper object. In other words, if the user enters "2", I want to be able to do something like: car-chosen: car2 [I know I can't just "join" the two values to create a pointer to car2] so that later in the script I can refer to the variables within car-chosen as print car-chosen/model which should display "Taurus". - martin

 [3/5] from: joel:neely:fedex at: 19-Feb-2002 15:27


Hi, Martin, Here's my suggestion... Martin Middleton wrote:
> Let's say I have the following: > > car: make object! [ > make: "Ford" > model: "Custom 500" > color: white > ] >
...
> And assume that my REBOL program asks the user the to enter a > number between 1 and 3 and that I save the response in something > called car-choice. > > How can I "combine" (probably a bad choice of terminology) the > value "car" and the user's response so that I end up with a value > I can use to refer to the proper object... >
Keep the cars in a block, as follows:
>> car: make object! [
[ make: "Ford" [ model: "Custom 500" [ color: white [ ]
>> cars: reduce [
[ make car [model: "Escort" color: red] [ make car [model: "Taurus" color: blue] [ make car [make: "Buick" model: "Riviera" color: silver] [ ] == [ make object! [ make: "Ford" color: 255.0.0 model: "Escort" ] m... The identifier can then be which car in the block:
>> repeat which length? cars [
[ print cars/:which/model [ ] Escort Taurus Riviera Alternately, you could add a "name" or "label" field to each object in the block and search the block for the object with the specified value of that field. -jn-

 [4/5] from: chalz:earthlink at: 19-Feb-2002 23:48


Joel's solution is the one which came to me first, too. But, I was thinking in C. Basically, an array which contains .. well, it's not the same as pointers in C, but basically close. You'd have to revert to some other methods of traversing and modifying the list, though, of course. The neat thing about REBOL, however, is you *can* do something like, say, you want to create 5 'car' instances. Instead of writing it all out manually, you can have code which runs 5 times and actually creates variables car1, car2, car3... Because of the way REBOL handles 'words and such, you can generate variable (and function) names on the fly. (You can also modify functions on the fly, too, if the code is so designed.) I'm afraid I lost my code snippets relating to such things, though. :/

 [5/5] from: joel:neely:fedex at: 20-Feb-2002 5:48


Hi, Charles, Charles wrote:
> Joel's solution is the one which came to me first, too. But, > I was thinking in C. >
I wasn't, I can assure you.
> Basically, an array which contains .. well, it's not the > same as pointers in C, but basically close. >
Not from my perspective. A REBOL block is a way to have a collection of "things" (whether objects or otherwise), and a block can be searched, traversed, etc. in a variety of ways. It has nothing to do with pointers. The original question had to do with taking a user-supplied value in the range of 1..3 and being able to refer to attributes of the n-th car. There are several advantages to doing this via a block (which is, after all, a very distinctively REBOL-is structure): 1) It is that it is clear what is going on. 2) Once the cars are in a block, one can do other useful things efficiently and with little effort, such as: 2.1) Extend the definition of a car to keep a counter of the number of times that it was selected. Then one can create a "usage report" via something as simple as foreach auto cars [print [auto/model tab auto/selected]] 2.2) Instead of asking the user to type a number, build a pick list or collection of radio buttons identified by e.g. model names and allow the user to make a selection that way. 3) The present implementation of REBOL has a hard limit on the number of words which may be defined. Manufacturing synthetic names for values uses up scarce resources. There's no point in doing so if REBOL contains perfectly usable, clear, and faster ways of managing a collection of the values in question. -jn- -- ; sub REBOL {}; sub head ($) {@_[0]} REBOL [] # despam: func [e] [replace replace/all e ":" "." "#" "@"] ; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"} print head reverse despam "moc:xedef#yleen:leoj" ;