General:This frame introduces
you to simple indexing principals in Glee. These examples
correspond to the examples given for numeric vectors. This demonstrates another
characteristic of Glee ... learn a concept and it holds for all
types.
Simple Indexing: From the
character string 'abcdef' the elements (7 6 5 4 3 2 1 0) are
extracted in that order yielding ( fedcba ). I use the
:hex method for strings to show the result in hexadecimal form. For
strings, invalid indexes yield spaces. This is inconsistent with what we
did with numbers. With numbers we assumed the value at the index and
nil outside the range of valid indices. This had some rationale for
numerics but for characters, space makes mores sense.
Insert Indexing: When you
index with a fractional index, this tells Glee you want to insert
elements into the result. The integer indices you already understand from the
previous example. But decimal indices tell Glee to open a space
for the value. In this example, 1.1 leaves a space in the result.
Notice there is nothing at vector position 0 so those indexes also
yield space. I also illustrate saving a value into a variable
('abcd'=>a;) as I did with the numeric examples. I can then reference
the variable (in this case "a") in the indexing operation as
(a[0 0.1 1 1.1 2 2.1 3 3.1 4 4.1]) yielding
( a b c d ). Again, the :hex
method makes the spaces easier to see.
Indexing with index
generators: We begin with the string ('abcdefghi'=>a;).
(a[2..5] $;) indexes the 2nd through 5th element using range.
(a[4` +1] $;) does the same using a calculated index. They both yield
the same (bcde).
Indexed Assignment: You can
use Indexed assignment to change the contents of variables. This example
shows three statements.
Statement 1: ('abcde'=>a;) creates a variable a.
Statement 2:('CD'=>[3 4]a;) assigns the elements of the
string 'CD'into the 3rd and 4th element of a.
Statement 3: (a:hex $; ) then displays the resulting
"a" after the changes yielding (abCDe).
Indexed Assignment:
Here with ('DCX'=>[4 3 9 2]a;) we have the case of more indexes
than data. Again, as with numbers, Glee does the left take
on the left argument to even things out. But where numbers drag the last
numeric value from the right, this isn't really appropriate for strings. So
strings drag spaces from the right giving "D" "C"
"X" "space". The "X" tries to go into element 9
(which is nowhere). The the 4th element of the left argument (now a space) is
indexed into the 2nd position of "a".
Showing Explicit Left
Take: This example produces the same result as the previous one. Only this
time we do the left take explicity ('DCX'<-4 $ ) and display the
intermediate result with the $ ("DCX ").
Boolean indexing (or
selection):In the numeric example, we created the boolean indexes using a
logical operation (e.g. >). Here I illustrate using
Glee's special boolean notation in a character string. If the
string contains only 1's and 0's and is used as an index, Glee
interprets it as a boolean vector. So ('abcdef'["11001101"])
yields (abef .) selecting at every 1 and rejecting at every 0.
Remaining consistent, indexing into an invalid position yields spaces.
Empty Index: As with all
Glee objects, an empty index causes a copy of the object's
contents to be returned, in this case ( abcdef )