1. to Rob (isn't this a bit silly)
- Posted by Alan Tu <ATU5713 at COMPUSERVE.COM>
Nov 27, 1998
-
Last edited Nov 28, 1998
This function is a function that I am using to trim off whitespace at the=
end of a sequence. I am forced to put a statement in there that will
never, never, never, never, and yes, never, be executed to make the
interpreter happy. Why am I faking out the interpreter? 'cause otherwis=
e
it wines. This defeats Euphoria's purpose of elegance.
global function rtrim(sequence string)
if string[length(string)] =3D ' ' then -- if last character
string =3D string[1..length(string)-1] -- if space, take it away
else
return string -- ok, last character isn't a space, done, get out
end if
string =3D rtrim(string) -- recursive call to check another character=
return string -- Now, please explain to me the elegance or purpose of=
-- having this statement
end function
Now, couldn't you make "attempting to exit from a function without return=
"
a run-time error instead of compile-time? If you can't, then I will take=
it as a limitation of Euphoria's mother language, C.
--Alan
=
2. Re: to Rob (isn't this a bit silly)
- Posted by Hawke <mdeland at NWINFO.NET>
Nov 27, 1998
-
Last edited Nov 28, 1998
Alan Tu wrote:
> Why am I faking out the interpreter?
> global function rtrim(sequence string)
<snip>
> end if
> string = rtrim(string) -- recursive call
> return string --why do i need this?
acutally that return WILL be executed....
you may find this next set of code fragments and functions
easier to read, understand, and follow....
and they might help you see why that return is needed...
global function Error(sequence msg)
--UNcomment this if you are going to use graphics modes...
--if graphics_mode(-1) then end if
clear_screen() puts(1,msg & "\n") abort (1)
end if
global function IsSame(object a,object b) return compare(a,b)=0 end
function
global function Delete(object data, integer where)
--this function will kill any character in a string
--OR
--any record in a sequence
--quite general purpose :)
if atom(data) then return "" end if
return data[1..where-1] & data[where+1..length(data)]
end function
global function RightTrim(object data, object target)
--this function will trim off the last 'thing' from
--data, if that 'thing' at the end of data matches target.
--'thing' is either a sequence of records,
-- (in which case target should be the record to trim)
--OR
--'thing' is a character string.
-- (in which case target should be the character to trim)
integer last
if atom(data) then
--Comment this out, once you have debugged the program,
-- unless target will be controlled by outside influences
-- during run time...
if sequence(target) then Error("RightTrim:bad param") end if
if data = target then return "" end if
return data
end if
last = length(data) --ixnay redundant length() checks
if not last then return "" end if --is a char/rec left????
if IsSame(data[last],target) then --begin trimming and recursing
data = Delete(data,last)
return RightTrim(data)
else
return data
end if
end function
global function RightTrimNUMBERTWO(object data, object target)
--potentially (a lot!?) faster than recursing...
integer last
if atom(data) then
if sequence(target) then Error("RightTrim:bad param") end if
if data = target then return "" end if
return data
end if
last = length(data)
if not last then return "" end if
while IsSame(data[last],target) do
data = Delete(data,last)
last = length(data)
if not last then return "" end if
end while
return data
end function
----------------------end
hope this helps.....
--Hawke'
3. Re: to Rob (isn't this a bit silly)
- Posted by JCMiura at AOL.COM
Nov 28, 1998
Alan Tu says:
> I am forced to put a statement in there that will
> never, never, never, never, and yes, never, be
> executed to make the interpreter happy.=A0
*ALL* of your statements are executed. The reason that
you get an error message is because by definition,
a function must return some value. You made a function,
thus you must return a value. It's that simple.
You can modify your function rtrim(). The following will do:
global function rtrim(sequence string)
=A0=A0=A0 if length(string) =3D 0 then
=A0=A0=A0=A0=A0=A0=A0 return string
=A0=A0=A0 elsif string[length(string)] =3D ' ' then -- if last character
=A0=A0=A0=A0=A0=A0=A0 return rtrim(string[1..length(string)-1]) -- recursi=
ve call here
=A0=A0=A0 else
=A0=A0=A0=A0=A0=A0=A0 return string -- ok, last character isn't a space, d=
one, get out
=A0=A0=A0 end if
end function
As you can see in the code above, you need to check if the
parameter string is a null string. Otherwise you may have a
runtime error.
Of course, you don't have to have a recursive function to perform
above. Just a while loop will do. But if you want to use a recursive
function, go ahead.
Regards,
=A0=A0=A0=A0 Junko C. Miura
=A0=A0=A0=A0 Rapid Deployment Software
=A0=A0=A0=A0 http://members.aol.com/FilesEu/
4. Re: to Rob (isn't this a bit silly)
>Regards,
>Junko C. Miura
>Rapid Deployment Software
>http://members.aol.com/FilesEu/
Hi Junko!
I think this is first time I see your technical support on
the listserver... welcome ;)
Regards,
Daniel Berstein
daber at pair.com
5. Re: to Rob (isn't this a bit silly)
Hi!
It's nice to see Junko posting here :)
Irv
6. Re: to Rob (isn't this a bit silly)
To remove unwanted spaces at the end of a (simple) string I use the
following routine.It would be easy to customize it to remove characters only
from the left or tight side.
-- " this is a " becomes "this is a"
global function remove_end_spaces(sequence word)
integer a,b
a=0
b=length(word)
if b then
for i = 1 to b do
if word[i]!=' ' then a=i exit end if
end for
if a then -- ok, at least one valid character detected
for i = b to a by -1 do
if word[i]!=' ' then b=i exit end if
end for
return word[a..b]
end if
end if
return {}
end function
-----Original Message-----
From: Alan Tu
>This function is a function that I am using to trim off whitespace at the
>end of a sequence. I am forced to put a statement in there that will
>never, never, never, never, and yes, never, be executed to make the
>interpreter happy. Why am I faking out the interpreter? 'cause otherwise
>it wines. This defeats Euphoria's purpose of elegance.
7. Re: to Rob (isn't this a bit silly)
>global function rtrim(sequence string)
> if string[length(string)] = ' ' then -- if last character
> string = string[1..length(string)-1] -- if space, take it away
> else
> return string -- ok, last character isn't a space, done, get out
> end if
> string = rtrim(string) -- recursive call to check another character
> return string -- Now, please explain to me the elegance or purpose of
>-- having this statement
>end function
>--Alan
As has already been stated, that line IS used. You might perhaps try:
global function rtrim(sequence string)
if string[length(string)] = ' ' then
string = string[1..length(string)-1]
return rtrim(string) <--------<<<
else
return string
end if
end function
I haven't checked it for speed, but it at least looks more elegant to me.
Isaac
"I am amused by the simplicity of this game. Bring me your finest meats
and cheeses."