Post Reply 
String Manipulation
10-15-2023, 01:11 AM (This post was last modified: 10-15-2023 01:39 AM by rickster.)
Post: #1
String Manipulation
I am running a virtual HP 48GX on Emu48: I'm trying to develop an FRP character generator.
I have strings like "ABC|BC|JKLMN|WXYZ". (Each capital letter represents a specification
for a PC skill: we are to pick one group at random and give the PC those skills.) I need
to delete the '|' bar and replace it with "\" \"", and wrap it in a list, so my object will read
{ "ABC" "BC" "JKLMN" "WXYZ" }. Now I can call DUP SIZE DIE GET on the list to get a
random group...

But I don't know how to insert that string in place of '|'. Any ideas?
Would it be easier to just start with { "ABC" "BC" "JKLMN" "WXYZ" }?

Charles? He was my grandfather. --Oh, *that* Charles. We share a common ancestor.
Find all posts by this user
Quote this message in a reply
10-15-2023, 12:33 PM
Post: #2
RE: String Manipulation
(10-15-2023 01:11 AM)rickster Wrote:  Would it be easier to just start with { "ABC" "BC" "JKLMN" "WXYZ" }?

If you can start with quoted strings, that would certainly be easier. Without more information on your intended application, it is difficult to make further suggestions.

If you can use an emulated 50g rather than a 48, the command SREPL would make the string conversion fast and easy. You could simulate SREPL with a program but it would be much slower.
Find all posts by this user
Quote this message in a reply
10-15-2023, 12:43 PM (This post was last modified: 10-15-2023 05:41 PM by Giuseppe Donnini.)
Post: #3
RE: String Manipulation
If lists are permitted by the logic of your program, it would certainly be easier.
If, on the other hand, you must use strings, here is a way (in User RPL) to convert them to appropriately formatted lists:

\<<
  DEPTH                 @ Record current stack depth.
  1 -                   @ Take input string into account.
  \-> s                 @ Save stack depth as lambda variable s.
  \<<
    WHILE               @ Start conditional loop.
      DUP "|" POS DUP   @ Keep going as long as we find "|" characters.
    REPEAT
      DUP2              @ Cut out next substring:
      1 -               @ - Compute position of last character.
      1                 @ - Position of first character is always equal to one.
      SWAP SUB          @ - Cut.
      ROT ROT           @ Crop remaining string:
      1 +               @ - Compute position of first character.
      MAXR              @ - Position of last character is always equal to +oo.
      \->NUM            @ - Safeguard in case constants are symbolic (default).
      SUB               @ - Crop.
    END
    DROP                @ Drop left-over result of POS (zero, by now).
    DEPTH s -           @ Compute number of list elements.
  \>>
  \->LIST               @ Build final list.
\>>

Your example:

( "ABC|BC|JKLMN|WXYZ" --> { "ABC" "BC" "JKLMN" "WXYZ" } )

Some special case examples:

( "|AB|CD" --> { "" "AB" "CD" } )
( "AB|CD|" --> { "AB" "CD" "" } )
( "AB||CD" --> { "AB" "" "CD" } )
( "|AB||CD|" --> { "" "AB" "" "CD" "" } )
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)