Split string: Difference between revisions
Jump to navigation
Jump to search
imported>Eliteofdelete No edit summary |
imported>Eliteofdelete Formatting should be fixed. |
||
Line 66: | Line 66: | ||
}} | }} | ||
{{CodeSample| | |||
description= | 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= | code= | ||
<syntaxhighlight> | <syntaxhighlight> |
Revision as of 05:30, 26 January 2015
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).