Shop OBEX P1 Docs P2 Docs Learn Events
String Lookup Routines — Parallax Forums

String Lookup Routines

Shane MShane M Posts: 58
edited 2009-09-09 15:14 in Propeller 1
Hello everyone.· I have a question. I can think up a few solutions but I don't want to write a lage amount of code to find there was a much more efficient way.

Bottom line I am accepting serial text strings that are 16 characters or less.· Matching the string with a much longer string that I will send via the emic text-to-speech chip.· Here is an example of strings that I am receiving:

"message1"
"message2"
"message3"

So if the string is "message1" I will have stored in·memory (data area)·somewhere "This is message one".· So that each time I receive the string "message1", I will lookup the matching text-to-speech text.

I already have the propellor receiving messages in one cog.· I have another cog checking a string variable -- if it has changed, I can run a sub routine (currently I just have the emic say the received message but here I will put the MessageLookup routine).·

I need to create·a subroutine that will match the received message with values in memory and then point me the to the memory position of the matching text string.

Any ideas or commands I'll find useful to do this?

I considered a "case"· or "lookdown" but it doesn't seem it will handle the string the way I need.

Can anyone give me some advice.

Thanks in advance.

·

Comments

  • SamMishalSamMishal Posts: 468
    edited 2009-07-31 01:50
    Shane,

    Look at the command StrComp page 203 in the manual.

    But also why not use numerics instead of "message 1" etc. why not use 1 or even just "1"
    this would of course off put the burden of deciding the string on the SENDER program which
    if is a PC can do more extensive string manipulation.

    Thus all you have to do is send one byte as a numeric 0-255 or as a character "1" etc.
    and then all you have to do on the propeller is use numerical comparison.

    However, look at the StrComp command if you really need to.



    Samuel
  • Shane MShane M Posts: 58
    edited 2009-07-31 03:02
    Hi. Thanks for your fast reply.

    Strcomp is what I considered and use in the code. I didn't know of an elegant way to lookup the text related to the message.

    I agree that a number would be easier. However I'm communicating with a PLC and the only option I have is ASCII RS-232 messages.

    Thanks!
  • kwinnkwinn Posts: 8,697
    edited 2009-07-31 03:17
    If there are only a few different messages then using a string compare might be the simplest way. However with a large number of strings it may be faster and requires less storage to use a hash algorithm on the incoming strings to generate an index into an array of pointers to the outgoing text to speech strings.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-07-31 15:18
    I made these (the attached) functions a while back, they may be what you are looking for.
    They mimic PHP functions of the same name. But there are string searching, parsing, etc.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
  • Shane MShane M Posts: 58
    edited 2009-07-31 20:07
    Thank you! Looks like a great library to have. I appreciate that.
  • Shane MShane M Posts: 58
    edited 2009-08-03 18:50
    Worked out great guys.· Thanks for the help.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-08-28 21:31
    I have created an object in the OBEX for the string file I posted.
    I made some changes. I sped things up as well as fixed substr to work more like PHP's function.

    You can download the latest version here: obex.parallax.com/objects/502/

    If anyone else has string function similar to this, it would be nice to have them all in one library on the OBEX, please post them, and I can add it into the file (cited of course).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
  • Shane MShane M Posts: 58
    edited 2009-08-29 00:46
    Thanks!
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-09-08 20:36
    In case anyone is interested. I have just released version 1.3 of my Strings Library Object.

    Changes since 1.0: Added StrReplace, Combine, StrRev, Trim, StrPad, Capitalize, StrRepeat, and StrParse. Fixed SubStr's negative values. Removed STR_MAX_LENGTH limit from StrPos. And major speed updates between each of the releases.

    Download it here: obex.parallax.com/objects/502/


    Anyone know why searching for "Strings Library" or "Strings" in the Obex does not return this object?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
  • Shane MShane M Posts: 58
    edited 2009-09-09 01:37
    Thanks Bobb. Much appreciated. Not sure why the search does not work?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Shane Merem
    www.websiteforge.com
    www.magnusoft.com

    ·
  • VIRANDVIRAND Posts: 656
    edited 2009-09-09 08:31
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-09-09 15:14
    VIRAND said...
    This might be interesting:

    http://forums.parallax.com/showthread.php?p=613308

    I fixed the link (above)...

    When I tried to fix it in the browser it took me to the HTTP Wikipedia page, weird.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
Sign In or Register to comment.