Re: Please explain use of $ sign in this excerpt from catch_keys.exw wxEuphoria demo.
- Posted by DerekParnell (admin) May 31, 2010
- 1288 views
Can anyone explain the use of the $ in the following code *msg[$])* and also the [1..$-1] and 1..$-2]* I understand the basics of this but the details of the $ sing I can't seem to find.
if keyIs != 27 then if keyIs = 13 then msg[$] = msg[$][1..$-1] msg = append(msg,"_") elsif keyIs = 8 then -- deleting if length(msg[$]) > 1 then msg[$] = msg[$][1..$-2] & "_" else if length(msg) > 1 then msg = msg[1..$-1] msg[$] &= "_" end if
Thanks Tim
The $ symbol is a shorthand way of saying length(TheSequence), or in other words - the last item. It is only allowed inside the square brackets and always refers to the same sequence that its enclosing brackets refer to.
Thus msg[$] and msg[length(msg)] mean the same thing.
And msg[1 .. $-1] and msg[1 .. length(msg)-1] also mean the same as each other.
It gets even more useful when sub-sequences are referenced.
msg[$][2..$] is the same as saying msg[length(msg)][2 .. length(msg[length(msg)])]
(In other words, fetch everything from the last item except the first sub-item.)
Two common idioms are S[1 .. $-1] and S[2 .. $] referring respectively to all except the last item and all except the first item.