Shop OBEX P1 Docs P2 Docs Learn Events
Can the Propeller support string variables or other useful string operations? — Parallax Forums

Can the Propeller support string variables or other useful string operations?

ElectricAyeElectricAye Posts: 4,561
edited 2008-09-08 06:18 in Propeller 1
Hi all,
I've been looking for a way to "increment" a string. In other words, I'm looking for a way to make string Name1 become string Name2, then Name2 become Name3, etc. in a loop thousands of times.

Does anyone know if the Propeller can do that? via Spin or assembly or some fancy-schmancy technique?

thanks,
Mark

PS.
(I asked a very similar question about incrementing SD card files earlier, but I thought that it would be better to re-ask this question in a more general way rather than be so specific about SD files.)

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-06 16:02
    You can do it in any language that supports arrays of characters. You could do it with a Stamp. You simply start on the rightmost digit and add one to it. If the result is "9" or less, you're done. If the result is larger than "9", set that character to "0" and move to the next character to the left and do it all again. If you ever move to a character position that doesn't contain a digit, stop.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2008-09-06 16:49
    Mike Green said...
    You simply start on the rightmost digit and add one to it.

    Thanks, Mike, it's nice to know that it can be done. Now, when you say "add" a digit to a string, how is that done in Spin? How in Spin do you set up arrays for characters?

    I'm still perplexed,
    Mark

    blush.gif
  • ElectricAyeElectricAye Posts: 4,561
    edited 2008-09-06 16:53
    I found out I could get this to work to write a file name with the csv extension, where Number is a constant. Problem is, I can't figure out how to increment Number.

    sdfat.popen(string("Name", Number, ".csv"), "w")

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    It might be the Information Age but the Eon of Ignorance has yet to end.
  • BTXBTX Posts: 674
    edited 2008-09-06 17:47
    Mark.
    Do you want to do this?

    r := sdfat.popen(@buffer, "r")

    Then load buffer with Name1, Name2, and so on.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Regards.

    Alberto.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-06 17:48
    A string in Spin is just an array of bytes with the last byte used containing the value zero.
    Look at the chapter on BYTE in the manual. You declare a byte array called foo for example of size 6 with
    VAR BYTE foo[noparse][[/noparse] 6 ]
    


    You can initialize it to "N0000" and increment it as follows
    To initialize, do: bytemove(foo,string("N0000"),6)
    Note that this also copies the zero byte that marks the end of the string.
    
    PRI increment(address,length)
    ' address is the starting address of the byte array for the string
    ' length is the total length of the byte array including the zero byte that marks the end of the string
      address += length - 2   ' Change the address so it points to the rightmost digit of the numeric value.
      repeat
        result := byte[noparse][[/noparse]address]   ' Use the implicit result variable for a temporary variable
        if result => "0" and result =< "9"   ' Check to make sure there's a digit character there
          byte[noparse][[/noparse]address] := ++result   ' Increment the character
          if result > "9"
            byte[noparse][[/noparse]address--] := "0"  ' If greater than "9", set to zero and carry a one
            next   ' Repeat the loop
        quit
    


    You'd call this with: increment(@foo,6)
  • ElectricAyeElectricAye Posts: 4,561
    edited 2008-09-06 18:04
    Mike, Alberto,
    Ah-haaaa...
    that's the kind of stuff I'm looking for. I'll have to ponder this today.

    Thanks you guys!
    Mark smile.gif
  • ElectricAyeElectricAye Posts: 4,561
    edited 2008-09-08 06:18
    Alberto, Mike,

    As usual, your suggestions are proving to be extremely valuable!
    For the record, though, in case anyone else is following this thread, the bytemove line needs an @ before foo.
    It should read,

    bytemove(@foo,string("N0000"),6)

    Thanks again!
    Mark smile.gif
Sign In or Register to comment.