Subscribe Bookmark RSS Feed

How to achieve this simple Column addition in JSL?

Hi,

Just a newbie of JSL.

I open a data tabel (each column is numeric type).
And want to add up each other column to a new column "sum".
Given the following script, I always get the Log Comment:
invalid argument in access or evaluation of 'Column' , Column( dt, j )
Can anyone help me out?
Thanks, Fred

---------
dt = currentdatatable();
nc = ncol(dt);
nr = nrow(dt);
show(nr);show(nc);
sumcol = dt << newcolumn("sum", numeric);
for(i=1, i<=nc, 1,
for(j=1, j<=nr, 2,
sumcol += column(dt, j);
)
)
2 REPLIES
Sorry.

My script actually was:

dt = currentdatatable();
nc = ncol(dt);
nr = nrow(dt);
show(nr);show(nc);
sumcol = dt << newcolumn("sum", numeric);
for(i=1, i<=nc, i++,
for(j=1, j<=nr, j++,
sumcol += column(dt, j);
)
)
XanGregg

Staff

Joined:

Jun 23, 2011

It looks like [ i ] triggers italics mode among other formatting oddities I don't yet understand in the forum software.

Possible issues: The sumcol and column(dt, j) need to be subscripted (presumably that was lost in the formatting). Each sumcol row needs to be initialized to 0 before adding things to it. It looks like i and j are mixed up, and the column iterator should stop before the last column because you don't want to add sumcol to itself.

In general, you can debug these sorts of issues by executing your script one line at a time and checking results.