Shop OBEX P1 Docs P2 Docs Learn Events
Parsing A string — Parallax Forums

Parsing A string

bdickensbdickens Posts: 110
edited 2010-07-25 02:28 in Propeller 1
I'm having to code well away from the hardware so if this is really basic, I apologize but I can't check it with the LCD right now.

I need to parse a string into 2 separate strings. The first 4 characters identify the data stream. Pretty sure the first datamove will do that. But the second, will this work, or do I need an @ in front of the InDataRec{4} ?

NOTE: Those are really square brackets after the InDataRec but I could not for the life of me figure out how to post.

Dat
inDataRec btye $FF, 50, "WR01THE REST "
First4 byte $FF, 4, " "
Lastpart byte $FF,46
Pub Start
Bytemove(First4,InDataRec,4)
Bytemove(LastPart,InDataRec{4},46)

Post Edited (bdickens) : 7/24/2010 7:06:52 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-24 19:00
    1) Add a subject to your posting. Use the pencil icon in the right upper corner of your first message to change it.

    2) It's hard to tell what you're doing. Please do not use cut-and-paste to include code in your message. Use the Attachment Manager that you get when you click on the Post Reply button. Alternatively, you can use the [noparse][[/noparse] code ] and [noparse][[/noparse] /code ] tags around your code. Note that other tags like [noparse][[/noparse] i ] will be stripped out by the forum software and will affect the formatting, not what you expect.

    Note that BYTEMOVE expects addresses of the byte arrays used for strings. Normally you get the address of a string item by using @item.
  • bdickensbdickens Posts: 110
    edited 2010-07-24 19:06
    Thanks. I was trying to fix that while you were commenting. Not as easy as it would seem
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-24 19:25
    Your pseudo-Spin code still doesn't really make sense. How about describing what you want to accomplish? What are you trying to move where with your BYTEMOVEs?

    The format of your BYTE statements doesn't make sense. Please look at the manual again. Are you trying to have multiple instances of the $FF byte values? If that's what you want, you have to put the count in square brackets after the value like: $FF[noparse][[/noparse] 4 ]

    Post Edited (Mike Green) : 7/24/2010 7:31:58 PM GMT
  • bdickensbdickens Posts: 110
    edited 2010-07-24 21:21
    All I was trying to do was what we would have done in basic with a substr(string,1,4) and a substr(string,5,30). Take one string and break it into two separate parts.

    I'll relook the data section.
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-07-24 21:40
    It looks like your $ff, 4 and $ff, 56 is some kind of message header including the length, right?

    You need to make the data section behind first4 and lastpart bigger. When you do a bytemove to first4 you will overwrite the lastpart section.

    dat

    first4 byte $ff, 4, 0[noparse][[/noparse]·5 ]
    lastpart byte $ff, 56, 0[noparse][[/noparse]57]

    the 0[noparse][[/noparse]·5 ]·will place 5 zeros in memory. Please note it's one more zero than needed for the 4 characters, so the last zero is the stringend character. This allows to use the SPIN string-functions. If you have your own methods which use the length given as the second byte, you can of course replace the 5 with a 4.

    Then I suppose what you want to do is:
    bytemove( @frist4+2, @inDataRec + 2, 4 )
    bytemove( @lastpart+2, @inDataRec + 6, 56 )

    the +2 skips the $ff, 4 and $ff , 56 as I think you want to keep this information.

    Post Edited (MagIO2) : 7/24/2010 9:45:44 PM GMT

  • bdickensbdickens Posts: 110
    edited 2010-07-24 22:04
    My mistake, we all seem to be focused on the dat section. Ignore that totally and lets start this again. Either I am into something really complex, or my explanation made it worse.

    I have a string InDataRec which happens to be 30 bytes long
    I have a string called First4
    and another called LastPart


    The first 4 bytes of InDataRec are a transaction header.
    The rest are the transaction itself.

    The routine is passed InDataRec. The contents of that string are "WR01TheStringThatCameIn1234568"

    I need to separate this into two strings. In basic, I would have coded this as

    First4 = substr(InDataRec,1,4)
    LastPart = substr(InDataRec,5,Len(InDataRec))

    And First4 would have been "WR01"
    and LastPart would have been "TheStringThatCameIn1234568"

    I agree, I did not do the DAT correctly and I appreciate all the input. So presuming I fix that to be 3 strings, how do I get this parsed.

    Thanks
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-24 22:15
    First, you'll probably declare the strings like this:

    VAR byte InDataRec[noparse][[/noparse] 31 ], First4[noparse][[/noparse] 5 ], LastPart[noparse][[/noparse] 27 ]

    The extra byte is for the zero byte that marks the end of any string

    You'd then do:

    BYTEMOVE( @First4, @InDataRec, 4) ' Copy the first 4 characters to First4. Note no checking is done.
    First4[noparse][[/noparse] 4 ]~ ' Force the last byte to be zero (after the 4 characters)

    and:

    BYTEMOVE( @LastPart, @InDataRec+4, STRSIZE(@InDataRec+4)+1) ' Copy the rest of the string and the zero terminator

    You could compare the prefix as follows:

    IF STRCOMP( @First4, STRING("WR01"))
    ' Do something if equal

    Post Edited (Mike Green) : 7/24/2010 10:20:38 PM GMT
  • bdickensbdickens Posts: 110
    edited 2010-07-25 02:28
    thats exactly what I needed. Thanks for being patient with the issue.
Sign In or Register to comment.