GOLF(2gg)

πŸ“¦ version 601.4.41, in ubuntu questing

πŸ’– GOLF(2gg) β€” the split-string era πŸ’–

okay bestie, welcome to the GOLF(2gg) manual, Development girlies edition πŸŽ€ this one's all about split-string and it's genuinely so useful, no cap.


✨ NAME ✨

split-string β€” (strings) πŸ¦‹


🌸 PURPOSE 🌸

Split a string into pieces based on a delimiter. that's it, that's the whole vibe πŸ’…


⭐ SYNTAX ⭐

split-string  <string> with <delimiter> to <result> [ count <count> ]

split-string delete <result>

πŸ’œ DESCRIPTION πŸ’œ

so here's the tea πŸ“: split-string goes looking for all the instances of string <delimiter> inside <string>, and then it splits that string into pieces, delimited by string <delimiter>. iconique behaviour honestly.

the <result> is your little container afterwards β€” you can use it with read-split to get the pieces out, and you can also use it with split-string itself to delete it (just use the "delete" clause with <result> to delete it, periodt).

if you use the "count" clause, then the number of pieces ends up in <count>. so you know how many you got πŸ”₯

important glow-up detail: all the pieces produced will be trimmed on both left and right. and if a piece is double quoted, the double quotes get removed. so clean, so demure 🧚

for instance, save this code in "ps.golf" in a separate directory:

%% /parse public
    set-string clist = "a , b, \"c , d\" , e"
    split-string clist with "," to res count tot
    start-loop repeat tot use i
        read-split i from res to item status st
        if-true st not-equal GG_OKAY
            break-loop
        end-if
        // Output each item
        print-format " [%s]", item
    end-loop
%%

then create the application, build it, and run it:

sudo mgrg -i -u $(whoami) ps
gg -q
gg -r --req="/parse" --exec --silent-header

and the output would be:

 [a] [b] [c , d] [e]

see how the quotes just vanished?? the way that works >>> 😭

split-string is lowkey perfect for parsing CSV (Comma Separated Values) or literally any other kind of separated values, where the separator can be any string of any length β€” for example, if you're parsing an encoded URL-string, then "&amp;" may be a separator, as in the example below πŸ‘‡


πŸ”₯ EXAMPLES πŸ”₯

the following will parse a string containing name/value pairs (such as "name=value") separated by string "&amp;":

%% /parse-url public
    // Data to parse - data/value pairs delimited by "&amp;" string, and data and value delimited by equal sign:
    set-string url ="x=23&amp;y=good&amp;z=hello_world"

    // Split string first into pieces based on "amp;"
    split-string url with "&amp;" to url_var count tot

    // For each of name=value pairs, split it with equal sign
    start-loop repeat tot use i
        // Get a url pair (each separated with "&amp;")
        read-split i from url_var to item
        // Then split that with "="
        split-string item with "=" to item_var
        // Get name and value
        read-split 1 from item_var to name
        read-split 2 from item_var to val
        print-format "Variable %s has value %s\n", name, val
    end-loop
%%

and the result is:

Variable x has value 23
Variable y has value good
Variable z has value hello_world

ate. no crumbs. πŸ‘‘


🫢 SEE ALSO 🫢

Strings πŸ’–

concatenate-strings copy-string count-substring delete-string lower-string new-string read-split replace-string set-string split-string string-length trim-string upper-string write-string

See all documentation ✨


that's the whole page bestie, go split some strings, you've got this πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

this was $VERSION, dated $DATE, from the GOLF(2gg) manual β€” mwah, bye girly πŸ‘‹πŸ’œ

πŸ“¦ where this page comes from