0

I am creating a JSL script for some of my datatables and need my function to act on a column.

I can get the function to act on the column during a plotting event, but not with standard operations.

Here's an example that works. This acts on the current datatable and plots up the Distribution for :Column1, which happens to be Nominal Character with 4 unique items in it.

a = Function(
        {col},                 // Function parameters
        {Default Local},       // Local variables
        Distribution(
            Nominal Distribution(
                Column( col ),
                Horizontal Layout( 1 ),
                Vertical( 0 )
            );
        );
    );

dt = Current Data Table();
a(Expr(:Column1));

Note the Expr() around :Column1. Without this, the Distribution function does not work.


What I'm trying to do is the following, but it doesn't work. What it should do is show the number of unique items in :Column1.

a = Function(
        {col},                 // Function parameters
        {Default Local},       // Local variables
        retval = associative array(col);
        Show(nitems(retval));
    );

dt = Current Data Table();
a(Expr(:Column1));

    Returns in Log Window:
    N Items(retval) = 0;        // Should be 4

If I run the script without trying to wrap it in a function, then it works just fine:

retval = associative array(:Column1);
Show(nitems(retval));

    Returns in Log Window:
    N Items(retval) = 4;        // My column happens to have 4 unique entries in it.

I'm fairly certain that my issue has something to do with namespace inside the function, but I can't seem to figure it out. Does anyone have any suggestions?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
dthor
  • 1,749
  • 1
  • 18
  • 45

1 Answers1

1

It is (from what I've seen) just an issue with the scoping operator : in :Column1.

Try using

a = Function(
    {col},                 // Function parameters
    {Default Local},       // Local variables
    retval = associative array(col<<Get Values)<<Get Keys;
    Show(nitems(retval));
);

dt = Current Data Table();
a(column(dt, "Column1"));

it returned

N Items(retval) = 9; 
Faller
  • 1,588
  • 3
  • 16
  • 27
  • That appears to have solved things. I also needed to use `retval = associative array( col << get values )` to get it to really work. But you sent me in the right direction, so thanks! – dthor Nov 21 '14 at 21:42
  • I'd use retval = associative array(col< – Faller Nov 21 '14 at 21:46
  • Associative arrays will have the same number of keys as they do values, so `nitems(associative array(col< – dthor Nov 21 '14 at 22:02