BasicMicro - Forums

www.basicmicro.com
It is currently Mon May 21, 2012 8:34 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: string comparison
PostPosted: Sat Oct 02, 2010 11:01 pm 
Offline
Citizen

Joined: Thu Aug 19, 2010 9:34 pm
Posts: 17
I wonder if there is an easy way to do string comparison on the Pro series.

IF mystring = "1234" THEN label

Can something like this be done, or do I have to parse the string byte-by-byte and do number of GOSUBs?


Top
 Profile  
 
 Post subject: Re: string comparison
PostPosted: Sun Oct 03, 2010 10:11 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 528
I don't know of any easy way to do this. This basic does not have any real sense of strings as variable types. That is, there are no length fields associated with them nor are there any defined termination characters. What strings are is simply an array of bytes.

I have played with this some in the past and if I am simply looking for short strings I end up doing something like:
if (MyString(0)="1") and (MyString(1)="2") ...

or if I am working with lots of strings or the like I do create subroutines for them. I would then setup my search strings as byte tables. As part of this I would define some standard way I am using the strings. Either my input string is assumed to have some trailing character. I would use that approach if I am for example looking for commands that were input from a terminal with commands like:
Code:
fEqual var byte
CMD1 bytetable  4,"CMD1"    ; approach if you wish to have first character be length
CMD2 bytetyable "CMD1",0   ; Approach if you wish to have trailing character to mark end
mystring var byte(80)
serin s_in, i9600, [MyString\80\13]

gosub StringComp[&mystring, 80, &cmd1], fEqual
...
p1 var pointer
cnt var byte
p2 var pointer
f var byte
StrComp[p1, cnt, p2]:
...  ; do code to find out if strings are equal...
return  f   ; returns equality...
I would know that my string was up to 80 characters long and if it terminates early it has a value of 13 in the last byte...

I did not write the code above to do the actual compare, it would depend on what approach you use to define your search strings. I passed in pointers as it makes the code flexible to reuse to compare different strings. You could also change this, to have a byte table that defined all of the strings you wish to compare against and have the function return an index number... If I were writing the string compare function I would probably do it using inline assembly language... But that is just me :D

Kurt


Top
 Profile  
 
 Post subject: Re: string comparison
PostPosted: Mon Oct 04, 2010 6:35 pm 
Offline
Citizen

Joined: Thu Aug 19, 2010 9:34 pm
Posts: 17
Thanks for your explanation. I'm not familiar with the microcontroller hardware or the assembly language for this particular micro. That's why I bought basic micro. Basic language doesn't require any hardware knowledge. Any micro should behave more or less the same. The beauty of these "modules" is that you just pop them into socket and you're ready to go. No soldering, nothing to worry about. I just wish the basic language had more commands. Yeah, I can get things done by writing more lines of code, but that should be job of the compiler. The compiled basic tokens execute much faster than bunch of basic gosubs. Imagine how many short assembly functions people wrote already. If they were named after basic tokens, there would be a whole basic compiler available. Many years ago someone did that for the PC. It was called "Spontaneous Assembly".

My task is fairly simple. All my strings would be the same length. I just need to setup table and use loop to parse them byte-by-byte like the example you included. Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO