Using split command to split a string

122 views Asked by At

I am looking to split a string and put it into a set. The string to be split is an element of a tuple.

This string element in the tuple takes values such as(pitblockSet) :

{"P499,P376,P490,P366,P129,"}
{"P388,P491,P367,"}
{"P500,P377,P479,P355,"}

and so on. Each set refers to a Path Id(string name of the path)

The tuple was defined as :

tuple Path {
string id;
string source;
string dest;
{string} pitblockSet;
{string} roadPoints; 
{string} dumpblockSet;
{string} others;
float dist;
};

And the above sets to be split refers to the element : {string} pitblockSet;

I now have to split the pitBlockSet. I am using the following :

{Path} Pbd={};

// Not putting the code to populate Pbd as it is irrelevant here
// there are several lines here for the purpose of creating set Pbd...

{string} splitPitBlocksPath[Pathid];
{string} splitDumpBlocksPath[Pathid];

execute {
for(var p in Pbd) {


var splitPitBlocksPath[p.id] = p.pitblockSet.split(",") ;

var splitDumpBlocksPath[p.id] = p.dumpblockSet.split(",") ;
 
}
}

The problem is when I execute it I get error in the above 2 lines appearing 4 times:

Scripting parser error: missing ';' or newline between statements.

I am not able to understand where I am going wrong

===============Added after Alex's Answer ============================= Thank you for the Answer again - It worked perfectly with some minor changes.

I might not have been able to explain the issue properly in the above hence adding the following. My actual code is much bigger and these are only an extract

Pbd for my case is a tuple of type {Path}. Path is described above. Pbd reads about 20,000 records from excel and each Pbd has tuple fields like id, source, dest, pitblockSet, dumpblockSet etc etc. These are all read from excel and populated into the tuple Pbd - this part is working fine. The 3 lines that I mentioned above were just for example of the Pbd.pitBlockSet for just 3 records out of the 20,000.

p.pitblockSet is a set but it contains only one string. The requirement is to break this string into a set. Like for example if p.pitblockSet has a value {"P499,P376,P490,P366,P129,"} for say p.id = "PT129" the expected result for this p.id is {"P499" "P376" "P490" "P366" "P129"}. Then say for example for p.id="PT1" the p.pitblockSet is {"P4,"} the expected result is a set with only one element like {"P4"}. As mentioned earlier there are several such records of p and the above two are just for example.

I have therefore modified the suggested code to some extent to fit into the problem. However I am still getting an issue with the split command.

 {string} result[Pbd];


int MaxS=10;


execute {
for(var p in Pbd) {


var stringSet = Opl.item(p.pitblockSet,0);


var split= new Array(MaxS);
split=stringSet.split(",") ;

for(var i=0;i<=MaxS;i++) if ((split[i]!='null') && (split[i]!='')) result[p].add(split[i]);

writeln("result:", p.id, result[p]);
 
}

}

The Answers look like below :

result:PT1 {"P4"}
result:PT2 {"P5"}
result:PT3 {"P6"}
result:PT4 {"P7"}
result:PT5 {"P8"}
result:PT6 {"P8" "P330" "P455" "P341"}
result:PT7 {"P326"}
result:PT8 {"P327"}
result:PT9 {"P328"}
.
.
and so on
.
.
result:PT28097 {"P500" "P377" "P479" "P355"}
result:PT28098 {"P501" "P378" "P139"}
result:PT28099 {"P501" "P388" "P491" "P367"}
result:PT28100 {"P501" "P378" "P480"}
result:PT28101 {"P501" "P378" "P139"}
result:PT28102 {"P502"}
result:PT28103 {"P503"}
2

There are 2 answers

1
Chris Bracchi On

Unfortunately, I'm afraid you encounter a product limitation. See: https://www.ibm.com/support/knowledgecenter/SSSA5P_12.10.0/ilog.odms.ide.help/refjsopl/html/intro.html?view=kc#1037020 Regards, Chris.

2
Alex Fleischer On
int MaxS=10;

{string} splitDumpBlocksPath={"P499,P376,P490,P366,P129,"} union
{"P388,P491,P367,"} union
{"P500,P377,P479,P355,"};

range Pbd=0..card(splitDumpBlocksPath)-1;

{string} result[Pbd];



execute {
for(var p in Pbd) {

var stringSet=Opl.item(splitDumpBlocksPath,p);
writeln(stringSet);

var split= new Array(MaxS);
split=stringSet.split(",") ;

for(var i=0;i<=MaxS;i++) if ((split[i]!='null') && (split[i]!='')) result[p].add(split[i]);


 
}
writeln(result);
}

works fine and gives

P499,P376,P490,P366,P129,
P388,P491,P367,
P500,P377,P479,P355,
 [{"P499" "P376" "P490" "P366" "P129"} {"P388" "P491" "P367"} {"P500" "P377"
         "P479" "P355"}]