Re: Last Element Notation

new topic     » goto parent     » topic index » view thread      » older message » newer message

Pete wrote:

> On Wed, 17 Sep 2003 14:15:24 -0400, Robert Craig
> <rds at RapidEuphoria.com> wrote:
>
>>
>> Unless someone has a better idea, I think I'll go ahead
>> with this idea of $ meaning "the index of the last element".
>> e.g.
>>    s[1..$]
>>    s[1..$-1]
>>    s[$-2]
>> etc.

Very nice!
Generally speaking, I would prefer the Greek omega character (well known
as symbol for 'end') to '$', but unfortunately the omega character isn't
included in the ASCII range from 0 to 127.

>> I've wanted to do something like this for quite a while,
>> but I always had the feeling there might be a better way.
>>
> Since you asked, I'm [still] sold on negative indexes:
>
> 	s="(0,0)"
> 	x=s[1..-1]		-- x is now "(0,0)"
> 	x=s[2..-2]		-- x is now  "0,0"
> 	x=s[3..-3]		-- x is now   ","
>
> 	s="abc.exw"
> 	if equal(s[1..3],"abc") then		-- this is true
> 	if equal(s[-3..-1],"exw") then		-- this is true
>
> 	s=s[3..-1]		-- removes the first two elements of s
> 	s=s[1..-3]		-- removes the last two elements of s
>
> 	s=s[1..2]		-- selects the first two elements of s
> 	s=s[-2..-1] 	-- selects the last two elements of s

I like negative indexes, too. But I don't know, whether or not I like
them more than the '$' notation. smile

I often have code such as the following:
   sequence s, t
   integer p
   s = "Hello there - have a nice day!"
   p = find('-', s) - 1
   if p = -1 then
      p = length(s)
   end if
   t = s[1..p]

If s[1..-1] means the same as s[1..length(s)], then this code snippet
could be written shorter:
   sequence s, t
   integer p
   s = "Hello there - have a nice day!"
   p = find('-', s) - 1
   t = s[1..p]

Or even shorter:
   sequence s, t
   s = "Hello there - have a nice day!"
   t = s[1..find('-', s)-1]

Cool, eh? smile
But I don't know, whether this might be confusing for Euphoria newbies.

> "end" may be a more readable alternative to $.

Yes.
I also have thought about EOS ('End Of Sequence'), but I don't regard
this as a good idea myself. smile

> When "end" is between [] should be no major parsing problem?
>
> If end/$ is internally treated as -1, this fits nicely with negative
> index handling.
>
> I think there is a nice symmetry there, even if no-one else does.

I also think so.

Regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |    |\      _,,,---,,_
 \ /  against HTML in       |    /,`.-'`'    -.  ;-;;,_
  X   e-mail and news,      |   |,4-  ) )-,_..;\ (  `'-'
 / \  and unneeded MIME     |  '---''(_/--'  `-'\_)

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu