RE: if statement not working

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

Graeme wrote:
> 
> >I just tripped against this stupid restriction again last night. I've
> written a function called 
> >begins() to help me out, but it ain't pretty.
> >
> >   if begins(s, t) then ...
> >
> >I originally wrote:
> >
> >   if equal(s[1..length(t)], t) then ...
> >
> >but this fails whenever s is shorter than t. 
> 
> 
> Yeah, and I hate having to press enter at the end of the
> line, it really annoys me. Rob can you fix this or I want my
> money back.....
> 
> 
> But seriously, having the interpreter return a valid result 
> from accessing non-existant data is just way stupid.
> 
> 
> Graeme

I agree with Graeme on this one :P

Here is a function that I sometimes use for formatting sequences.

function format(sequence s, sequence form)
   if length(s) < length(form) then
      return s & form[length(s)+1..length(form)]
   elsif length(s) > length(form) then
      return s[1..length(form)]
   end if
   return s
end function

Dereks problem would be written:
 if equal(format(s,t),t) then ...

(I'd much rather write:  if format(s,t) = t then ... )


In particular, I find it handy for declaring my routines like this:

procedure foo(sequence args)
   args = format(args,{1,0,{},"none"})

   etc...
end procedure

This allows the routine to be called with a dynamic number of arguments.
If some of the trailing arguments are omitted, they are appended using 
the defaults.

This also lends to expandability, and a bunch of other convenient 
things.


Chris

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

Search



Quick Links

User menu

Not signed in.

Misc Menu