I want to convert a string like “1,3-5,8-10” to an array [1,3,4,5,8,9,10]. I have looked at the regexp and regexp2, and can match the digits, but am having trouble creating the range between the digits. The link shows it done in php, but I cannot find any squirrel examples.
`function parse(string1){
local data1=split(string1,",");
local build="";
foreach (count, value in data1) {
local sr=value;
local ex = regexp("[-]");
if(ex.search(value)){
local data2=split(value,"-");
sr=data2[0].tointeger();
local st=data2[1].tointeger();
while(sr<=st){
build=build+sr+",";
sr++;
}
}
else{
build=build+sr+",";
}
}
local results=split(build,",");
foreach(val in results)
server.log("value="+val+"\
");
}
local test1="1,3-5,8-10,21,43";
parse(test1)`
I reckon you could do this with just 2 splits. Not a lot of error checking with this. Probably should be enclosed with try/catch
`
myString <- "1,2-5,36"
result <- []
foreach(n in split(myString,",")) {
local range = split(n,"-")
if (range.len()==1)
result.push(n.tointeger())
else
for ( local val=range[0].tointeger();val<=range[1].tointeger();val++)
result.push(val)
}
//to test
server.log(“result=”+http.jsonencode(result))
`