THE PUZZLET PAGE


How_Does_It_Work


FUNCTION:  RemoveTrailingZeroes().  This function takes a string (whose characters will usually be numerical) as its argument, removes any trailing zeroes from the argument, and returns the truncated string. If its argument is, say, "34587000" it will return "34587".

Declaring the function must be done like this:

declare RemoveTrailingZeroes(f$: string)

Here's the code:

sub RemoveTrailingZeroes(r$)
' removes all trailing zeroes from argument r$
' and returns truncation to calling routine
def flag, L: int
L = len(r$): 'get length of argument
flag = -1: 'set flag to TRUE (-1)
do
    if right$(r$, 1) = "0": 'is there a trailing zero?
        L = L -1: 'decrement length indicator
        r$ = left$(r$, L): 'remove rightmost character
    else: 'no trailing zero
        flag = 0: 'set flag to FALSE (0)
    endif
until not flag: 'quit when flag is FALSE
return r$: 'return truncated string


Variable L initially holds the length (number of characters) of argument r$ and is then used to iterate over the rightmost digits of r$, removing each one that is found to be a zero.  The truncated result is returned to the calling routine.

I've numbered the lines of code to make it easier to follow the description which follows.  Here is the code with line numbers.

1
2
3
4
5
6
7
8
9
10
11
12

def flag, L: int
L = len(r$): 'get length of argument
flag = -1: 'set flag to TRUE (-1)
do
    if right$(r$, 1) = "0": 'is there a trailing zero?
        L = L -1: 'decrement length indicator
        r$ = left$(r$, L): 'remove rightmost character
    else: 'no trailing zero
        flag = 0: 'set flag to FALSE (0)
    endif
until not flag: 'quit when flag is FALSE
return r$: 'return truncated string

Now for a line-by-line exegesis of the code. 

1

def flag, L: int.  Establises local variables.  flag will be used to indicate whether the last character examined was a zero or otherwise.  Variable L initiallyholds the length (number of characters) of argument r$ and is then used to iterate over the rightmost digits of r$.

2

L = len(r$).  Variable L is initialised to hold the length of the argument, r$.

3

flag = -1.  Variable flag is initialised to hold value TRUE (-1).  In this code, that arbitrarily means the last character of r$ is a zero.  Whenever the last character is found to be non-zero, flag's value will be reset to FALSE (0).

4

do.  Opens main DO loop.

5

if right$(r$, 1) = "0".  Line 5 uses IBasic's in-built function right$().  So it's effectively asking the question "Is the rightmost character of argument r$ a zero?"

6

L = L -1.  Lines 6 and 7 are only executed if the answer to the question in line 5 is "yes."  This means that the last character of r$ is a zero, and must therefore be reduced. That reduction will make r$ one character less, and so variable L, which holds the length of r$, must be decremented by 1 to remain correct.  This line takes care of that.

7

r$ = left$(r$, L).  Now the unwanted zero must actually be discarded.  The newly-decremented value in L can be used for this task, along with the inbuilt function left$().  If you look carefully at line 7, you can see it can be paraphrased as "Take L characters from the left end of r$ and save them back into r$, over-writing what was already stored in there."

Remember, before line 7 executes, the value stored in variable L is actually one less than the current length of r$, because L was decremented just prior to this by line 6.  So taking L characters from the left end of r$ really means taking all the characters in r$ except the rightmost one!

Here are a couple of examples:

r$
Value of L
r$ after execution of line 7
"98754000"
7
"9875400"
"9875400" 6
"987540"
"987540" 5
"98754"


8

else.  Indicates there is an alternate action if the answer to the question in line 5 was "no."

9

flag = 0.  And here's the alternate action - reset variable flag to FALSE (0).  This means that flag is now effectively indicating that the last digit of r$ examined was NOT a zero.

10

endif.  Housekeeping - closes IF-ENDIF clause opened in line 5.

11

until not flag.  We have now reached the end of the DO loop and a decision must be made: execute the loop again, or move on?  It all depends on whether all the trailing zeroes have been found and removed.  that will be indicated when the last one looked at was a non-zero, and that information is stored in flag.

Thus, line 11 effectively says "if flag isn't TRUE, quit to loop, otherwise execute it again."

12

return r$.  At this point, all trailing zeroes have been removed from r$.  All that remains is to return it in this new, truncated form to the calling routine, which is what line 12 does for us.


MAIN MENU
HOW DOES IT WORK?

Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 5th, 2010.