问题描述
我正在寻找分割字符串并将其放入集合中的方法。要拆分的字符串是元组的元素。
元组中的此字符串元素采用诸如(pitblockSet)的值:
{"P499,P376,P490,P366,P129,"}
{"P388,P491,P367,"}
{"P500,P377,P479,P355,"}
元组定义为:
tuple Path {
string id;
string source;
string dest;
{string} pitblockSet;
{string} roadPoints;
{string} dumpblockSet;
{string} others;
float dist;
};
上面要拆分的集合是指元素:{string} pitblockSet;
我现在必须拆分pitBlockSet。我正在使用以下内容:
{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(",") ;
}
}
问题是,当我执行它时,我在上述两行中出现了4次错误:
我无法理解我要去哪里哪里
===============在Alex的回答后添加============================ = 再次感谢您的回答-进行了一些细微的更改,效果很好。
我可能无法在上面正确解释该问题,因此添加了以下内容。我的实际代码更大了,这些只是摘录
对于我的情况,Pbd是{Path}类型的元组。路径如上所述。 Pbd从excel读取大约20,000条记录,每个Pbd都有元组字段,例如id,source,dest,pitblockSet,dumpblockSet等。这些都从excel中读取并填充到元组Pbd中-这部分工作正常。我上面提到的3行仅是Pbd.pitBlockSet的示例,其中20,000条中只有3条记录。
p.pitblockSet是一个集合,但仅包含一个字符串。要求是将此字符串分成一组。例如,如果p.pitblockSet具有值{“ P499,P376,P490,P366,P129,”},例如p.id =“ PT129”,则此p.id的预期结果为{“ P499”“ P376”“ P490“” P366“” P129“}。然后说,例如对于p.id =“ PT1”,p.pitblockSet为{“ P4,”},则预期结果是一组只有一个元素的集合,例如{“ P4”}。如前所述,有多个这样的p记录,而上面两个仅作为示例。
因此,我在某种程度上修改了建议的代码以适合该问题。但是我仍然遇到split命令问题。
{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]);
}
}
答案如下:
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"}
解决方法
很遗憾,恐怕您遇到产品限制。 参见:https://www.ibm.com/support/knowledgecenter/SSSA5P_12.10.0/ilog.odms.ide.help/refjsopl/html/intro.html?view=kc#1037020 问候, 克里斯。
,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);
}
工作正常并给出
P499,P388,P500,[{"P499" "P376" "P490" "P366" "P129"} {"P388" "P491" "P367"} {"P500" "P377"
"P479" "P355"}]