大家好,
我有幾個專欄:
1-“開始日期”列
2-“結束日期”列
通常,屬於組 (2) 的每一列也應屬於組 (1),反之亦然。 所以我們應該在兩組中有相同數量的列。
但是,屬於第 1 組的列不屬於第 2 組,反之亦然。
例子: “開始日期 9899”,但“結束日期 9899”列不存在。
目標是讓這兩組相交,以便兩組中有相同的列。
我受到以下代碼的啟發:Names Default To Here(1);
dt = Current Data Table();
// build
aa_duree = Associative Array();
aa_nonduree = Associative Array();
For Each({col_name}, dt << Get Column Names("String"),
If(Word(1, col_name) == "date début",
aa_duree[Word(-1, col_name)] = col_name;
, !IsMissing(Regex(col_name, "")),
aa_nonduree[col_name] = 1;
,
continue()
);
);
show(aa_duree);
show(aa_nonduree);
aa_duree_copy = aa_duree;
aa_duree_copy << Intersect(aa_nonduree);
cols_to_keep = Insert(aa_duree_copy << get keys, aa_duree_copy << get values);
aa_duree_copy2 = aa_duree;
aa_nonduree_copy = aa_nonduree;
aa_duree_copy2 << remove(aa_nonduree);
aa_nonduree_copy << remove(aa_duree);
cols_to_remove = Insert(aa_nonduree_copy << get keys, aa_duree_copy2 << get values);
show(cols_to_keep, cols_to_remove); 您提供的示例腳本將不起作用,因為 Word(1, col_name) 只會返回字符串的第一個作品,而您正在將它與兩個單詞進行比較。 因此 if 語句將始終轉到 else 語句。 可能還有其他問題,但這至少是一個問題。 您可以嘗試將 Words 例如更改為 Starts With() https://www.jmp.com/support/help/en/17.0/#page/jmp/character-functions-2.shtml?os=win&source=application#ww2489516 . 還要確保比較字符串時大小寫正確
這是我解決這個問題的老派方法
Names Default To Here( 1 );
dt = Current Data Table();
keep = {};
delete = {};
colNameList = dt << get column names( string );
For Each( {col, i}, colNameList, colNameList[i] = Uppercase( col ) );
While( N Items( colNameList ) > 0,
If(
Contains( colNameList[1], Uppercase( "Date début" ) ),
found = Contains( colNameList, Substitute( colNameList[1], Uppercase( "début" ), "FIN" ) );
If( found > 0,
Insert Into( keep, colNameList[1] );
Insert Into( keep, colNameList[found] );
Remove From( colNameList, found, 1 );
Remove From( colNameList, 1, 1 );
,
Insert Into( delete, colNameList[1] );
Remove From( colNameList, 1, 1 )
);,
Contains( colNameList[1], Uppercase( "Date fin" ) ),
found = Contains( colNameList, Substitute( colNameList[1], "FIN", Uppercase( "début" ) ) );
If( found > 0,
Insert Into( keep, colNameList[1] );
Insert Into( keep, colNameList[found] );
Remove From( colNameList, found, 1 );
Remove From( colNameList, 1, 1 );
,
Insert Into( delete, colNameList[1] );
Remove From( colNameList, 1, 1 )
);,
Remove From( colNameList, 1, 1 )
)
);
show( keep, delete );