Ciao a tutti,
Ho un datatable con più colonne, alcune delle quali voglio aggiungere insieme. Come parte del mio script, seleziono alcune colonne, memorizzo i nomi delle colonne in un elenco e quindi spero di aggiungere le colonne selezionate utilizzando la funzione "Somma".
Per fare ciò ho bisogno di costruire una stringa ":DATA1, :DATA2, :DATA3" e analizzarla in una colonna.
New Column( "Addition", Numeric, Continuous, Formula( Parse( Sum( :DATA1, :DATA2, :DATA3 ) ) ) );
Di seguito è riportato uno snippet del codice che sto utilizzando:
New Column( "Addition", Numeric, Continuous, Formula( Parse( Sum( :DATA1, :DATA2, :DATA3 ) ) ) );
list = {"DATA1", "DATA2", "DATA3"};
For( i = 1, i <= N Items( list ), i++,
If(
i == 1, string = ":" || Char( Eval( list[Eval( i )] ) ),
i > 1, string = string || "," || ":" || Char( Eval( list[Eval( i )] ) )
)
);
Parse( string );
All'esecuzione, ottengo il seguente errore:
Inaspettato ",". Forse manca un ";" O ",".
Riga 1 Colonna 7: :DATA1►,:DATA2,:DATA3
Il testo rimanente che è stato ignorato è stato
,:DATI2,:DATI3
:DATI1
C'è qualcosa che mi manca?
Grazie in anticipo!
Grazie per l'aiuto e la spiegazione! La colonna "Yet Another Data" nel tuo script fa esattamente quello che voglio. La colonna "Another Data" sembra restituire una cella vuota con un errore "necessario che gli argomenti siano scalari, matrici o elenchi ..". Cercherò di capire perché questo sta accadendo.
Guarda la stringa in cui ritorna.
":DATI1,:DATI2,:DATI3"
Prova a eseguire solo quello senza le virgolette, otterrai la stessa cosa.
Se hai i nomi delle colonne come stringhe, puoi fare qualcosa di simile.
Names default to here(1);
dt = New Table( "Example",
Add Rows( 1 ),
New Column( "Data1", Set Values( [1] )),
New Column( "Data2", Set Values( [2] )),
New Column( "Data3", Set Values( [3] )),
);
// list of column names as strings
list = dt << Get Column Names("string"); // {"Data1", "Data2", "Data3"}
SumExpr = Expr(Sum());
for(i=1, i<=nitems(list), i++,
insert into(SumExpr, Column(dt, list[i]));
);
// see what the sumExpr looks like
show(nameexpr(sumExpr));
// then you have to do some substitute in order to actually get it to run
just_for_show = EvalExpr(
dt << New Column("Another Data", Formula(Expr(nameexpr(SumExpr))));
);
show(nameexpr(just_for_show));
just_for_show();// this will actually run the expression you just created.
// or you could use the list in Sum Directly.
// list of column names as references
list = dt << Get Column Names();
list=list[1::3];
// can do a list directly in Sum()
// should still substitute it in so it's no dependant on then list variable
just_for_show =Substitute(
Expr(
dt << new Column("Yet Another Data", Formula(Sum(DV_LIST)))
),
Expr(DV_LIST), list
);
show(nameexpr(just_for_show));
just_for_show(); // actually evaluates again.