Author Topic: String SELECT - tip&trick  (Read 1033 times)

0 Members and 1 Guest are viewing this topic.

Aurel

  • Guest
String SELECT - tip&trick
« on: April 22, 2019, 12:38:09 AM »
I often forget how to use SELECT with string type
and some things not work as it should be
so i was tested all possible options
and i found one strange things....

in some cases
byte b at strptr(s)

select b

....


work without problems but in this one not:

Code: [Select]
'str pointer - select
#autodim off
string s = "Oxy"
'print p + len(s)
byte b at strptr(s)

select b

case "basic"
print "NO!"

case "OXYGEN"
    print "NO!"

case "Oxy"
    print "YES"

end select

print "ok"

so you can try..just print OK

then i try to use INT instead of byte:
Code: [Select]
'selection with INT
'string casting -> select/filtering

string s="SELECT_STRING_WITH INTEGER" ' more than 4 chars
'(i AS string pointer)
INT i at strptr s  ' integer pointer cast string s

select i
case "SELECT_STRING_WITH INTEGER"
print "OK..this WORK!"

case "PRINT"
print "OK..just to compare"

    case else
       print " Error : Unknown selection!-> "

end select

print "OK!"

also work if type is SYS

Code: [Select]
'selection with INT
'string casting -> select/filtering

string s="SELECT_STRING_WITH INTEGER" ' more than 4 chars
'(i AS string pointer)
SYS i at strptr s  ' integer pointer cast string s

select i
case "SELECT_STRING_WITH INTEGER"
print "OK..this WORK!"

case "PRINT"
print "OK..just to compare"

    case else
       print " Error : Unknown selection!-> "

end select

print "OK!"

Aurel

  • Guest
Re: String SELECT - tip&trick
« Reply #1 on: April 22, 2019, 01:06:26 AM »
Here is example using macro function:

Code: [Select]
'selection with INT
'using macro function
macro strSelect(s)
sys p at strptr s
select p
case "Oxygen"
print "NO"
case "oxyGen"
print "OK-SELECTED"
case else
print "Unknown selection!"
end select
end macro

'input string...
string s = "oxyGen"
'call macro...
strSelect(s)

Arnold

  • Guest
Re: String SELECT - tip&trick
« Reply #2 on: April 23, 2019, 12:42:48 AM »
Hi Aurel,

I cannot imagine that your approach will work properly in the long run. Reading Oxygen Help file and the demos \examples\basics\Select.bas, Switch.o2bas, SelectAscii.o2bas I can see that only numeric values like sys, int, byte etc. are used with the select / switch case block which corresponds to the C standard.

I noticed that this topic has been already discussed:

Why SELECT not respond on string variable? 
https://www.oxygenbasic.org/forum/index.php?topic=1499.msg16329#msg16329

and look at reply# 8:
"Careful! Only the first 4 characters are tested, as an integer"

Probably the simplest way in Oxygenbasic is to use the if .. then .. elseif .. else .. endif conditional block for comparing strings (see reply#1).
 

Aurel

  • Guest
Re: String SELECT - tip&trick
« Reply #3 on: April 23, 2019, 04:39:52 AM »
Hi Arnold

Quote
I cannot imagine that your approach will work properly in the long run.

Yes i suspect that ..so i tested it in my microA tokenizer program
and also in my ANIscript program .
https://www.oxygenbasic.org/forum/index.php?topic=1485.0
https://www.oxygenbasic.org/forum/index.php?topic=1866.0
And i both cases work very good .  :)
And you can try it.

I know that Charles tell me that is only 4 char tested but from what i see it works with larger strings too.