你好,
我有一個資料庫查詢,它會傳回一個大表,包含 200 多個列,並且想在使用之前清理它。
這些欄位的初始術語表示計量工具來源,例如拉曼、XRD、XRF、BET、PSA...
對於每個術語,我想建立一個群組並使用上面列表中的名稱作為群組的名稱。
例如,有 20 個以術語「拉曼」開頭的列,我希望將它們全部分組在一起。
我應該如何編寫它的腳本?我嘗試使用名稱開頭(“Raman”)創建一個列表,但這似乎返回一個空列表。
附註:我問了 ChatGPT 但沒用。
安迪
這部分取決於您的數據,但總體思路通常是
- 取得您的列的列表
- 循環列表,同時將列收集到適當的群組中
- 循環適當的群組並建立列組
For Each() 用於循環,Associative Array() 用於資料結構,可能還有 Starts With() 用於檢查群組(或 Word(1,...))
這就是我開始的地方,不,這是行不通的。
Names Default to Here(1);
dt = Current Data Table();
CL = dt << Get Column Names();
Raman={};
For Each ( {value,index},CL,if(starts with(value,"Raman"),Raman<<INSERT item=""></INSERT>Next steps
Andy
幾個問題:
- 您希望對所有列進行分組嗎?
- 您的欄位是否有清晰的群組分隔符,例如底線(Raman_)
Names Default To Here(1);
dt = Current Data Table();
CL = dt << Get Column Names();
Raman = {};
For Each({value}, CL,
If(Starts With(value, "Raman"),
Insert Into(Raman, value);
);
);
dt << Group Columns("Raman", Raman);
以及基於我擁有的示例的完整示例
Names Default To Here(1);
dt = New Table("Untitled 2",
Add Rows(1),
Compress File When Saved(1),
New Column("Raman1", Numeric, "Continuous", Format("Best", 12), Set Values([.])),
New Column("Raman2", Numeric, "Continuous", Format("Best", 12), Set Values([.])),
New Column("XRD1", Numeric, "Continuous", Format("Best", 12), Set Values([.])),
New Column("Raman3", Numeric, "Continuous", Format("Best", 12), Set Values([.])),
New Column("XRD2", Numeric, "Continuous", Format("Best", 12), Set Values([.]))
);
collist = dt << Get Column Names("String");
aa_groups = Associative Array();
aa_groups["Raman"] = {};
aa_groups["XRD"] = {};
For Each({groupname}, aa_groups << get keys,
For Each({value}, collist,
If(Starts With(value, groupname),
Insert Into(aa_groups[groupname], value);
);
);
);
For Each({{groupname, cols}}, aa_groups,
dt << Group Columns(groupname, cols);
);
