1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
.TH GOLF 2gg $VERSION $DATE Development Tools
.SH NAME
split-string \- (strings)
.SH PURPOSE
Split a string into pieces based on a delimiter.
.SH SYNTAX
.RS 4
.EX
split-string <string> with <delimiter> to <result> [ count <count> ]
split-string delete <result>
.EE
.RE
.SH DESCRIPTION
split-string will find all instances of string <delimiter> in <string> and then split it into pieces delimited by string <delimiter>. The <result> can be used with \fBread-split\fP to obtain the pieces and with split-string to delete it (use "delete" clause with <result> to delete it).
If "count" clause is used, then the number of pieces is in <count>.
All pieces produced will be trimmed both on left and right. If a piece is double quoted, then double quotes are removed. For instance save this code in "ps.golf" in a separate directory:
.RS 4
.EX
%% /parse public
set-string clist = "a , b, \[char92]"c , d\[char92]" , 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
%%
.EE
.RE
Create the application, build and run it:
.RS 4
.EX
sudo mgrg -i -u $(whoami) ps
gg -q
gg -r --req="/parse" --exec --silent-header
.EE
.RE
The output would be:
.RS 4
.EX
[a] [b] [c , d] [e]
.EE
.RE
split-string is useful for parsing CSV (Comma Separated Values) or any other kind of separated values, where separator can be any string of any length, for example if you're parsing an encoded URL-string, then "&" may be a separator, as in the example below.
.SH EXAMPLES
The following will parse a string containing name/value pairs (such as "name=value") separated by string "&":
.RS 4
.EX
%% /parse-url public
// Data to parse - data/value pairs delimited by "&" string, and data and value delimited by equal sign:
set-string url ="x=23&y=good&z=hello_world"
// Split string first into pieces based on "amp;"
split-string url with "&" 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 "&")
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\en", name, val
end-loop
%%
.EE
.RE
The result is:
.RS 4
.EX
Variable x has value 23
Variable y has value good
Variable z has value hello_world
.EE
.RE
.SH SEE ALSO
Strings
\fBconcatenate-strings\fP
\fBcopy-string\fP
\fBcount-substring\fP
\fBdelete-string\fP
\fBlower-string\fP
\fBnew-string\fP
\fBread-split\fP
\fBreplace-string\fP
\fBset-string\fP
\fBsplit-string\fP
\fBstring-length\fP
\fBtrim-string\fP
\fBupper-string\fP
\fBwrite-string\fP
See all
\fBdocumentation\fP
|