CIAO,
Ho una query di database che restituisce una tabella di grandi dimensioni, con oltre 200 colonne, e vorrei ripulirla prima di lavorarci.
Le colonne presentano termini iniziali che indicano la fonte dello strumento di metrologia, ad esempio Raman, XRD, XRF, BET, PSA,...
Per ciascuno di questi termini vorrei creare un gruppo e utilizzare il nome presente nell'elenco sopra come nome del gruppo.
ad esempio ci sono 20 colonne che iniziano con il termine Raman e vorrei che fossero tutte raggruppate.
Come dovrei scriverlo? Ho provato a creare un elenco usando Name starts with("Raman") ma sembra che restituisca un elenco vuoto.
Nota a margine: ho chiesto a ChatGPT ma è stato inutile.
Andy
Ciò dipende in parte dai tuoi dati, ma l'idea generale di solito è
- Ottieni l'elenco delle tue colonne
- scorrere l'elenco mentre si raccolgono le colonne in gruppi appropriati
- scorrere i gruppi appropriati e creare i gruppi di colonne
Per Each() per il looping, Associative Array() per la struttura dati ed eventualmente Starts With() per il controllo dei gruppi (o Word(1,...))
È da qui che inizio e no, non funziona.
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
Alcune domande:
- Desideri raggruppare tutte le colonne?
- Le tue colonne hanno un separatore chiaro per il gruppo, ad esempio un trattino basso (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);
E un esempio completo basato sugli esempi che ho
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);
);
