Re: Is subscripting a slice useful

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

On Fri, 8 Oct 2004 14:26:12 -0700, codepilot Gmail Account
<codepilot at gmail.com> wrote:

>Right now subscripting a slice is not allowed.
>
>}}}
<eucode>
>constant asdf={"asdf","asdf","asdf","asdf"}
>? asdf[1..2][1]
></eucode>
{{{

>I think that this code should print out {97,97,97,97}.

I think you just shot yourself in the foot blink)

There is *no* way that asdf[1..2] should return anything other than a
sequence of length 2, and hence a nested subscript of that could not
possibly yield a sequence of length 4. What, in contrast, would you
expect asdf[1..4][1] to print? (Surely something else!)

This whole idea (which has been extensively discussed before) is so
open to misinterpretation it should definitely NOT be "standard".
Every application needs something different.

That said, it is not difficult. If you want to extract something from
a complex sequence then I'll happily write a function for you, given a
suitable example. If you wanted {97,97} then:

constant asdf={"asdf","asdf","asdf","asdf"}

function subscript(object o, sequence what)
object this
	this=what[1]
	what=what[2..length(what)]
	if sequence(this) then
		if length(this)!=2 then ?9/0 end if
		for i=this[1] to this[2] do
			o[i]=subscript(o[i],what)
		end for
		return o[this[1]..this[2]]
	end if
	if length(what) then
		return subscript(o[this],what)
	end if
	return o[this]
end function

? subscript(asdf,{{1,2},1})


prints {97,97}

Let me know if this does not do exactly what you want.

Regards,
Pete
PS I'd be quite surprised if anyone could prove this was significantly
slower than doing it "native". The cost is entirely in the creation of
the new sequence (aka result); the subscripting/function calls pale
into insignificance. CMIIW, if you can.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu