Difference between revisions of "Split string"
imported>StDoodle (Created page with '{{ #vardefine:name|split_string}}{{ #vardefine:return_type|string [int]}}{{ #vardefine:aggregate|yes}}{{ FunctionPage| name={{#var:name}}| function_category=String Handling Rout…') |
imported>Fredg1 |
||
(7 intermediate revisions by 5 users not shown) | |||
Line 6: | Line 6: | ||
FunctionPage| | FunctionPage| | ||
name={{#var:name}}| | name={{#var:name}}| | ||
− | |||
function1={{Function| | function1={{Function| | ||
Line 24: | Line 23: | ||
parameter2={{Param|string|delimiter}}| | parameter2={{Param|string|delimiter}}| | ||
p1desc={{Pspan|source}} is the string to split| | p1desc={{Pspan|source}} is the string to split| | ||
− | p2desc={{Pspan|delimiter}} is the (optional) | + | p2desc={{Pspan|delimiter}} is the (optional) [[Regular Expressions|regular expression]] to split with| |
}}| | }}| | ||
function_description=Returns an array of your {{pspan|source}}, split by either {{pspan|delimiter}} or by line-breaks if not supplied.| | function_description=Returns an array of your {{pspan|source}}, split by either {{pspan|delimiter}} or by line-breaks if not supplied.| | ||
− | + | code1={{CodeSample| | |
+ | title=Code Samples| | ||
+ | description=The following example shows how split string can easily convert comma separated strings into useful data.| | ||
+ | code= | ||
+ | <syntaxhighlight> | ||
+ | string items, numbers, total_string; | ||
+ | int total; | ||
+ | item thing; | ||
+ | string[int] split_map; | ||
+ | numbers = "1, 203, 20002, 450, 194, 92992, 9000, 1"; | ||
+ | items = "mae west, asshat, hell ramen, bird, batblade"; | ||
− | + | split_map = split_string(numbers, ","); | |
+ | print("You have a total of "+count(split_map)+" numbers.", "blue"); | ||
+ | print("They are the following:", "blue"); | ||
+ | foreach it in split_map { | ||
+ | print(split_map[it]); | ||
+ | total+=to_int(split_map[it]); | ||
+ | } | ||
+ | total_string = to_string(total, "%,d"); | ||
+ | print("Their total is: "+total_string+".", "blue"); | ||
+ | print(""); | ||
+ | //Items | ||
+ | clear(split_map); | ||
+ | split_map = split_string(items, ","); | ||
+ | print("You have a total of "+count(split_map)+" items.", "blue"); | ||
+ | print("They are the following:", "blue"); | ||
+ | foreach it in split_map { | ||
+ | thing = to_item(split_map[it]); | ||
+ | if (thing == $item[none]) | ||
+ | print(split_map[it]+" is not a valid item", "red"); | ||
+ | else | ||
+ | print(thing); | ||
+ | } | ||
+ | </syntaxhighlight>| | ||
+ | moreinfo= | ||
}} | }} | ||
+ | |||
+ | {{CodeSample| | ||
+ | description=The following example uses KoLMafia's proxy record of an consumable to return the min-max values of the consumable's moxie/muscle/mysticality/adventure gains. Using split string, it finds the average of each gain and returns it in a record. | | ||
+ | code= | ||
+ | <syntaxhighlight> | ||
+ | record item_range { | ||
+ | float mox; | ||
+ | float mus; | ||
+ | float mys; | ||
+ | float adv; | ||
+ | item thing; | ||
+ | }; | ||
+ | item_range stats; | ||
+ | |||
+ | item_range item_avg(item consumable) { //Input food/drink/spleen | ||
+ | item_range attributes; | ||
+ | string[int] avgmap; | ||
+ | float realavg; | ||
+ | string range_string; | ||
+ | attributes.thing = consumable; | ||
+ | if (consumable.fullness == 0 && consumable.inebriety == 0 && consumable.spleen == 0){ | ||
+ | print(consumable+" is not a valid consumable.", "red"); | ||
+ | return attributes; | ||
+ | } | ||
+ | |||
+ | //Moxie | ||
+ | range_string = consumable.moxie; | ||
+ | avgmap = split_string(range_string, "-"); | ||
+ | if (count(avgmap) == 1) | ||
+ | attributes.mox = to_int(range_string); | ||
+ | else { | ||
+ | foreach it in avgmap | ||
+ | realavg += to_int(avgmap[it]); | ||
+ | attributes.mox = (realavg/2); | ||
+ | } | ||
+ | realavg = 0; | ||
+ | clear(avgmap); | ||
+ | |||
+ | //Muscle | ||
+ | range_string = consumable.muscle; | ||
+ | avgmap = split_string(range_string, "-"); | ||
+ | if (count(avgmap) == 1) | ||
+ | attributes.mus = to_int(range_string); | ||
+ | else { | ||
+ | foreach it in avgmap | ||
+ | realavg += to_int(avgmap[it]); | ||
+ | attributes.mus = (realavg/2); | ||
+ | } | ||
+ | realavg = 0; | ||
+ | clear(avgmap); | ||
+ | |||
+ | //Mysticality | ||
+ | range_string = consumable.mysticality; | ||
+ | avgmap = split_string(range_string, "-"); | ||
+ | if (count(avgmap) == 1) | ||
+ | attributes.mys = to_int(range_string); | ||
+ | else { | ||
+ | foreach it in avgmap | ||
+ | realavg += to_int(avgmap[it]); | ||
+ | attributes.mys = (realavg/2); | ||
+ | } | ||
+ | realavg = 0; | ||
+ | clear(avgmap); | ||
+ | |||
+ | //Adventures | ||
+ | range_string = consumable.adventures; | ||
+ | avgmap = split_string(range_string, "-"); | ||
+ | if (count(avgmap) == 1) | ||
+ | attributes.adv = to_int(range_string); | ||
+ | else { | ||
+ | foreach it in avgmap | ||
+ | realavg += to_int(avgmap[it]); | ||
+ | attributes.adv = (realavg/2); | ||
+ | } | ||
+ | realavg = 0; | ||
+ | clear(avgmap); | ||
+ | return attributes; | ||
+ | } | ||
+ | |||
+ | stats = item_avg($item[mae west]); | ||
+ | print("Item "+stats.thing+" is a "+item_type(stats.thing)+" which gives the following averages:", "green"); | ||
+ | print("Moxie--"+stats.mox+", Muscle--"+stats.mus+", Mysticality--"+stats.mys+", and Adventures--"+stats.adv+".", "blue"); | ||
+ | </syntaxhighlight>| | ||
+ | moreinfo= | ||
+ | }}| | ||
+ | |||
+ | special=This function technically returns an array, not a map. The information in both is accessed in the same way. However, you cannot add new key-value pairs to an array (its length is static once set).<br></p> | ||
+ | {{CodeSample| | ||
+ | description=This also means that the array will always contain at least 1 key (0), even if it was supplied an empty string. As such, using [[count]] on the result is not a fully reliable way to know how many items were in a list.| | ||
+ | code= | ||
+ | <syntaxhighlight> | ||
+ | print( split_string("", ",").count() ); | ||
+ | print( split_string("a", ",").count() ); | ||
+ | print( split_string("a,b", ",").count() ); | ||
+ | print( split_string("a,b,c", ",").count() ); | ||
+ | print( split_string("a,b,c,d,e,f,g", ",").count() ); | ||
+ | </syntaxhighlight>| | ||
+ | moreinfo=Will return: 1, 1, 2, 3, 7 | ||
+ | }}<p> | ||
+ | | | ||
+ | }} | ||
+ | |||
+ | [[Category:String Handling Routines]] |
Latest revision as of 00:41, 21 August 2020
Function Syntax
string [int] split_string(string source )
string [int] split_string(string source ,string delimiter )
- source is the string to split
- delimiter is the (optional) regular expression to split with
Returns an array of your source, split by either delimiter or by line-breaks if not supplied.
Code Samples
The following example shows how split string can easily convert comma separated strings into useful data.
string items, numbers, total_string;
int total;
item thing;
string[int] split_map;
numbers = "1, 203, 20002, 450, 194, 92992, 9000, 1";
items = "mae west, asshat, hell ramen, bird, batblade";
split_map = split_string(numbers, ",");
print("You have a total of "+count(split_map)+" numbers.", "blue");
print("They are the following:", "blue");
foreach it in split_map {
print(split_map[it]);
total+=to_int(split_map[it]);
}
total_string = to_string(total, "%,d");
print("Their total is: "+total_string+".", "blue");
print("");
//Items
clear(split_map);
split_map = split_string(items, ",");
print("You have a total of "+count(split_map)+" items.", "blue");
print("They are the following:", "blue");
foreach it in split_map {
thing = to_item(split_map[it]);
if (thing == $item[none])
print(split_map[it]+" is not a valid item", "red");
else
print(thing);
}
The following example uses KoLMafia's proxy record of an consumable to return the min-max values of the consumable's moxie/muscle/mysticality/adventure gains. Using split string, it finds the average of each gain and returns it in a record.
record item_range {
float mox;
float mus;
float mys;
float adv;
item thing;
};
item_range stats;
item_range item_avg(item consumable) { //Input food/drink/spleen
item_range attributes;
string[int] avgmap;
float realavg;
string range_string;
attributes.thing = consumable;
if (consumable.fullness == 0 && consumable.inebriety == 0 && consumable.spleen == 0){
print(consumable+" is not a valid consumable.", "red");
return attributes;
}
//Moxie
range_string = consumable.moxie;
avgmap = split_string(range_string, "-");
if (count(avgmap) == 1)
attributes.mox = to_int(range_string);
else {
foreach it in avgmap
realavg += to_int(avgmap[it]);
attributes.mox = (realavg/2);
}
realavg = 0;
clear(avgmap);
//Muscle
range_string = consumable.muscle;
avgmap = split_string(range_string, "-");
if (count(avgmap) == 1)
attributes.mus = to_int(range_string);
else {
foreach it in avgmap
realavg += to_int(avgmap[it]);
attributes.mus = (realavg/2);
}
realavg = 0;
clear(avgmap);
//Mysticality
range_string = consumable.mysticality;
avgmap = split_string(range_string, "-");
if (count(avgmap) == 1)
attributes.mys = to_int(range_string);
else {
foreach it in avgmap
realavg += to_int(avgmap[it]);
attributes.mys = (realavg/2);
}
realavg = 0;
clear(avgmap);
//Adventures
range_string = consumable.adventures;
avgmap = split_string(range_string, "-");
if (count(avgmap) == 1)
attributes.adv = to_int(range_string);
else {
foreach it in avgmap
realavg += to_int(avgmap[it]);
attributes.adv = (realavg/2);
}
realavg = 0;
clear(avgmap);
return attributes;
}
stats = item_avg($item[mae west]);
print("Item "+stats.thing+" is a "+item_type(stats.thing)+" which gives the following averages:", "green");
print("Moxie--"+stats.mox+", Muscle--"+stats.mus+", Mysticality--"+stats.mys+", and Adventures--"+stats.adv+".", "blue");
Special
This function technically returns an array, not a map. The information in both is accessed in the same way. However, you cannot add new key-value pairs to an array (its length is static once set).
This also means that the array will always contain at least 1 key (0), even if it was supplied an empty string. As such, using count on the result is not a fully reliable way to know how many items were in a list.
print( split_string("", ",").count() );
print( split_string("a", ",").count() );
print( split_string("a,b", ",").count() );
print( split_string("a,b,c", ",").count() );
print( split_string("a,b,c,d,e,f,g", ",").count() );
Will return: 1, 1, 2, 3, 7