使用 word( ) 函数和指定分隔符的混淆大家好,
我正在尝试操纵我的列名。 使用“查找/替换”执行一次或两次非常容易,但我想编写脚本。
本质上,我想将列名称从“Singlets/Live leukocytes/Lymphocytes/B cells/A+B+C-,Freq. of Parent”更改为“ B 细胞/A+B+C-,频率。父母”。
这是我当前的代码:
dt = Current Data Table ();
colList = dt << get column names(string);
For ( i = 1, i <= N Items(colList), i++,
If (
contains(colList[i], "Singlets/Live leukocytes/Lymphocytes/"), <BR />
Column(dt, colList[i]) << set name (Word(2, colList[i], "Lymphocytes/"))
));
如果我单独运行这段代码的一部分,这就是我得到的:
代码行:
结果脚本日志
colList[i]:
"Singlets/Live leukocytes/Lymphocytes/B cells/A+B+C-,Freq. of Parent"
contains(colList[i], "Singlets/Live leukocytes/Lymphocytes/"):
1
Word(2, colList[i], "Lymphocytes/"):
"iv"
Word(3, colList[i], "Lymphocytes/"):
" l"
Word(4, colList[i], "Lymphocytes/"):
"uk"
Word(9, colList[i], "Lymphocytes/"):
"f Par"
显然,我的定界符没有按我预期的方式工作。 有任何想法吗?
我最初尝试做 Words(4, colList[i], "/") 但这只给了我“B 细胞”,因为在那之后还有另一个 / 。 与其他编程语言不同,我不知道是否有一种方法可以在 JSL 中指定“第 4 个单词,然后是所有内容,直到最后”。 我认为这是不可能的,所以我更改了分隔符,认为这可以解决我的问题。
非常感谢您的任何建议!
回复:混淆使用 word( ) 函数和指定的分隔符哦! 好的,这也使脚本指南中的示例之一变得更有意义!!
我最终使用 substr 对我有利! 我会在午餐后发布代码!
回复:混淆使用 word( ) 函数和指定的分隔符一个混淆是 WORDS 函数的定界符参数意味着如果您发现这些字符中的任何一个,则拆分此文本。
这种方法可能会让你更接近你想要的:
collist = {};
collist[1] = "Singlets/Live leukocytes/Lymphocytes/B cells/A+B+C-,Freq. of Parent";
collist[2] = "Singlets/Live leukocytes/Lymphocytes/Another Text";
collist[3] = "Singlets/Live leukocytes/Lymphocytes/Hello World";
remove_text = "Singlets/Live leukocytes/Lymphocytes/";
len_text = length(remove_text);
for (i = 1, i <= nitems(collist), i++,
start_pos = contains(colList[i], remove_text);
if (start_pos,
end_pos = start_pos + len_text;
new_name = substr(collist[i], end_pos);
show(new_name);
Column(dt, colList[i]) << set name (new_name);
);
);
回复:混淆使用 word( ) 函数和指定的分隔符分隔符是一个字符,而不是字符串。
您可能还会发现正则表达式在此问题中很有用。
回复:混淆使用 word( ) 函数和指定的分隔符使用 N Items( list ) 确定存在多少个级别并从末尾开始。
回复:混淆使用 word( ) 函数和指定的分隔符另外,您知道为什么原始代码不起作用吗?
回复:混淆使用 word( ) 函数和指定的分隔符如果我的列表项数量不相等怎么办? 这将是 list[4]||" "||list[5] 但如果我的列表中有 6 个项目并且我希望它到最后......
我正在尝试对一些遍历 N 项但还没有得到它的循环进行伪编码。
回复:混淆使用 word( ) 函数和指定的分隔符请参阅帮助 > 脚本索引 > 函数 > 字符 > 单词
回复:混淆使用 word( ) 函数和指定的分隔符使用“/”作为分隔符。 另外,改用 Words() 函数。 你会得到一份清单。 然后您可以连接列表项。 在这种情况下,新字符串将是 list[5] ||列表[5]。
很简单,是吧?