替換字串中的字符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() 將其重新組合在一起。
