替换字符串中的字符JMP 社区您好,
我正在尝试编写一个 jsl 代码,该代码将采用一个包含字符串的列,并在每一行中将计数替换为计数列中的值。 最终结果应该类似于“结果”列中的内容
有什么建议么?
自动化和脚本编写回复:替换字符串中的字符非常感谢贾莫
回复:替换字符串中的字符我认为问题出在这一行
x[4] = Char( arra[binxx[j]] );
我将修改循环以循环行而不是关联数组+行:
Names Default To Here(1);
dt = Current Data Table();
/// Get list of all the bins
binxx = Associative Array(:Bins[23 :: N Rows(dt)]) << Get Keys;
delcols = {"", " ", "}"};
/// Remove garbage values
For(i = 1, i <= N Items(delcols), i++,
Remove From(binxx, As List(Loc(binxx, delcols[i])));
);
///Get rows where Bins column contains BIN =
rows = dt << get rows where(Contains(:Bins, "BIN = "));
total = {};
//Loop to calculate the count of each bins
For(i = 1, i <= N Items(binxx), i++,
Insert Into(total, N Items(dt << get rows where(:Bins == binxx[i])))
);
//create array to store bin and counts
arra = Associative Array(binxx, total);
//loop to modify the character count in the table
For(i = 1, i <= N Items(rows), i++,
x = Words(:Bins[rows[i]]);
curbin = Substitute(x[3], "\!"", "");
If(Contains(arra, curbin),
x[4] = Char(arra[curbin]);
:Bins[rows[i]] = Concat Items(x);
,
continue();
);
);回复:替换字符串中的字符@jthi
我使用 Cragis 逻辑来更新字符串中的计数,但它不会替换正确的计数字符串。 我不确定我做错了什么。 你能看一下我的代码吗?
分配不正确的计数
这是代码:
dt = Current Data Table();
/// Get list of all the bins
binxx = Associative Array( :Bins[23:: N Rows( dt )] ) << Get Keys;
delcols = {"", " ", "}"};
/// Remove garbage values
For( i = 1, i <= N Items( delcols ), i++,
Remove From( binxx, As List( Loc( binxx, delcols[i] ) ) );
);
///Get rows where Bins column contains BIN =
rows = dt << get rows where( Contains( :Bins, "BIN = " ) );
total = {};
//Loop to calculate the count of each bins
For( i = 1, i <= N Items( binxx ), i++,
Insert Into( total, N Items( dt << get rows where( :Bins == binxx[i] ) ) )
);
//create array to store bin and counts
arra = Associative Array( binxx, total );
//loop to modify the character count in the table
For( j = 1, j <= N Items( binxx ), j++,
For( i = 2, i <= N Items( rows ), i++,
If( Contains( arra, Substr( :Bins[i], 8, 1 ) ),
x = Words( :Bins[rows[i]] );
x[4] = Char( arra[binxx[j]] );
:Bins[rows[i]] = Concat Items( x );
)
)
);
回复:替换字符串中的字符谢谢克雷格和贾莫
回复:替换字符串中的字符正则表达式是另一种选择
Regex(:Results, "(BIN = \!".+\!") (\d+)(\!".+)", "\1 " || Char(:Count) || "\3")
但在这种情况下,我会像 Craige 建议的那样使用 Words() + Concat Items() (如果模式始终相同)
回复:替换字符串中的字符这些字符串似乎遵循相同的模式。 也许使用 Words() 函数并以空格或 " 作为分隔符。 然后使用 concatitems() 将其重新组合在一起。
