Resolving Strings

Blitz3D Forums/Blitz3D Beginners Area/Resolving Strings

_PJ_(Posted 2003) [#1]
I know Blitz will automatically resolve avriables into string format where needed

i.e.


integer=50

Text 100,200,"A Number: "+integer



but what about the other way around?

I have a problem with the following:



integer$="50"

test=10

If test<integer Then answer$="LESS"

If test=integer Then answer$="EQUAL"

If test>integer Then answer$="MORE"

Print answer$



Now try the same but change test=10 to test=100.....


GNS(Posted 2003) [#2]
integer$="50" 

test=50 
test2=integer$

;If test<integer Then answer$="LESS" 

If test<test2 Then answer$="LESS"

;If test=integer Then answer$="EQUAL" 

If test=test2 Then answer$="EQUAL"

;If test>integer Then answer$="MORE" 

If test>test2 Then answer$="MORE"

Print answer$ 

WaitKey()


It works both ways I believe. You just can't compare integers/floats to strings directly. Notice I created a new integer and set it equal to whatever integer$ equals. Blitz does the conversion and now you can make comparisons. :D


_PJ_(Posted 2003) [#3]
Ah - great. I'm glad that's all it was, not a fault within Blitz :)


Odds On(Posted 2003) [#4]
I think it's probably doing a string comparison.

a is greater than b
d is less than h

etc.

so comparing 50 to 10 would be the same as comparing 50 to 100000 because 5 is greater than 1.


soja(Posted 2003) [#5]
You can use Int to change the string to an int:
integer$="50"
test=100
If test<Int integer Then answer$="LESS"
If test=Int integer Then answer$="EQUAL"
If test>Int integer Then answer$="MORE"
Print answer$



Oldefoxx(Posted 2003) [#6]
When you do an ASSIGN statement, Blitz makes the effort to
convert the right hand form into a suitable adaptation for the left hand form. An assign statement is represented by a "=" .

A comparison process, such as with an IF or SELECT statement, also uses a "=" symbol to compare two items together. But since the result is not going to be reassigned from right to left, there is no conversion done.

If, like C or C++, you used a different symbol to represent a comparason ("==" instead of just "="), it is easy to make this mistake, to assume that such a conversion takes place.

Actually, it should be possible to consider making such an effort to convert righthand form to lefthand form before making a comparason, but at present it appears that Blitz does not do this. So for the moment, it appears to just work as designed, and this would be a design feature.


Koriolis(Posted 2003) [#7]
But since the result is not going to be reassigned from right to left, there is no conversion done.
Wrong. As would have said my professors, you can't compare tomatoes with bananas. Ther IS a conversion done: as correctly stated by Fullernator, 'test' is promoted to string before doing the comparison, thus yielding to a result that Malice didn't expect (ie an alphabetical comparison, not an integer comparison).
In Blitz whenever you have an operator that operates on 2 values, values are promoted to the type of "most importance", where the order is as follows:
- int
- float
- string
(Note that ther is never any promotion from or to type instances).
Then, and only then, the operation is done.
Thus in the example of Malice, 'test' must indeed be promoted to a string before applying the '<' operator.

You can test this easily by modifying a bit the code
integer$="10"
test=10
As expected you will get "EQUAL", but now if you do
integer$="10   "
test=10
you will get "LESS", because the spaces at the end make the string "10 " be greater (in the alphabetic sense) than "10" (which is the conversion to a string of 10).


Floyd(Posted 2003) [#8]
This is where I jump in and issue my standard reminder.

All of this is explained in detail in the Language Reference.

See the 'Expressions' page.