Problem with a nested Trim Function

Just starting out? Need help? Post your questions and find answers here.
iancrt
User
User
Posts: 12
Joined: Sun May 18, 2003 10:55 am
Location: UK, LEEDS

Problem with a nested Trim Function

Post by iancrt »

Found a problem when using Trim() nested within another function. The simple code below should return :
4019
4019
4019

but instead I get:
4076
4019
4019

Seems very strange, has anyone else had this problem? Is there reason for this type of behaviour? Is it generally safe to nest up functions like this or better to keep them seperate? This is the first time I have hit a problem with nested functions in purebasic.....

Code: Select all

OpenConsole()
    ClearConsole()
    ;part one, using Trim within nested statement
    ECDAR=0
    ECDAR=ECDAR + Val(Trim(Mid("N4019      ",2,10)))
    PrintN(Str(ECDAR))
    ;part two, remove trim completely
    ECDAR=0
    ECDAR=ECDAR + Val(Mid("n4019      ",2,10))
    PrintN(Str(ECDAR))                                    
    ;part three, break nested statement into seperate lines
    ECDAR=0
    ECDARS.s=""
    ECDARS = Mid("n4019      ",2,10)
    ECDARS=Trim(ECDARS)
    ECDAR=Val(ECDARS)
    PrintN(Str(ECDAR))                                    
    ;part4, keep on screen until key is pressed
    PrintN("(Press a key to exit)")
    Repeat 
    Until Inkey()
CloseConsole()
AlGonzalez
User
User
Posts: 53
Joined: Sun Feb 15, 2004 6:04 am
Location: Easley, SC, USA

Post by AlGonzalez »

Very Odd,

I've added the following debug lines:

Code: Select all

Debug Mid("N4019      ",2,10)
Debug Trim(Mid("N4019      ",2,10))
Debug Val(Trim(Mid("N4019      ",2,10)))
Debug ECDAR + Val(Trim(Mid("N4019      ",2,10)))
Debug Str(ECDAR + Val(Trim(Mid("N4019      ",2,10))))
It seems to be an issue when combining Val, Trim and some addition because the results for the above are:

Code: Select all

4019  
4019
4019
4076
4019
And it is not consistent since the calculation in the fourth line, that gives the unexpected result, then gives the expected result in line five!

Curious :?
Post Reply