Ultranewb
Ultranewb

Reputation: 61

Using direct values in Rebol?

I'm a Rebol newb. I'm often finding situations where for some reason an expression needs to have a value as a variable instead of "direct use." I suspect that I'm just not using a switch or dereference operator correctly?

An example:

>> "hi"/1 ; doesn't work
== /1

>> x: "hi"
== "hi"
>> x/1 ; works
== #"h"

Any way I can make this and other examples work with "direct use?"

Thanks.

Upvotes: 1

Views: 149

Answers (3)

moliad
moliad

Reputation: 1503

Each datatype has its own lexical representation which allows you to interact with its internal data (or not).

There is no actual path syntax or operator in REBOL. This is mainly due to fact that Rebol is not an OOP language, even if it does have an Object! datatype.

You might not have noticed that the code FOO/BAR is not an expression at all, but rather a single data element which is actually of a path! *datatype*

>> FOO: context [BAR: "WTF!"]
>> type? first [ FOO/BAR ]
== path!

Only paths allow you to navigate Rebol data "literally". This only occurs when paths are _evaluated_ . In that case you will get the value which is obtained by navigating from that path.

>> type? do [ FOO/BAR ]
== string!

In Rebol, the generic datatype "accessors" are not lexical but programmatic in nature. As Sunanda showed you, you use the various series functions to manipulate literal data directly.

this is very powerful since they can be "chained", for example:

>> x: head remove back back back tail "-|_|-"
== "-||-"

I'll finish by giving you a teaser into more advanced path usage... path expressions!

>> blk: ["12" "34" "56" "78" "90" "ab" "cd" "ef"]
>> skip-size: 2
>> item: 3
>> blk/(skip-size * item - 1)/2
== #"0"

Note that you can really put any code in the paren! if it follows data which can be navigated.

Upvotes: 1

Gregory Higley
Gregory Higley

Reputation: 16558

I'm not an expert, but my understanding is that expressions with / marks in them are called paths, and must begin with a word! and not a literal — what you are calling a "direct value" — of any kind.

You can think of / as syntactic sugar for pick and select when you have a word! that serves as a variable. You cannot say "foo"/bar because "foo" is a string!, not a word!, but you can say foo/bar because foo is a word!.

Upvotes: 1

Sunanda
Sunanda

Reputation: 1555

Try these:

>> first "hi"    ;; also SECOND, THIRD, .... , NINTH
== #"h"

>> pick "hi" 1   ;; PICK var INDEX-VALUE 
== #"h"

>> x: "hi"       ;; like FIRST
>> last x
== #"i"

>> y: 2          ;; use Y as an index
>> pick x y      ;; same as pick x 2
== #"i"

>> x/:y          ;; the Yth char in X
== #"i"

Upvotes: 2

Related Questions