cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
] />

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
SpannerHead
Level VI

For Each Row with Insert Into

I'm struggling with an insert into statement inside a For Each Row iteration.  I want to look up a category from the column "Type" and based on that select the value in the column "COLUMN_NAME" to form a list for each category.  I think I'm failing with the Insert Into

// Get the current data table
dt = Current Data Table();

colName = "Type";

// Get the column reference
Types = Column( colName );

// Extract all values from the column
allValues = Types << Get Values;

// Get unique values (removes duplicates)
Cats = Associative Array( allValues ) << Get Keys;

// Sort the unique values (optional)
Cats = Sort List( Cats );

// Display results in the log
Show( Cats );

delCats = {"", "-"};
 

For( i = 1, i <= N Items( delCats ), i++,

                Remove From( Cats, As List( Loc( Cats, delCats[i] ) ) )

);

 

show(Cats);


For For( i = 1, i <= N Items( Cats ), i++,

Spud = Char(Cats(i));

Spud = {};

For Each Row(
    // Access column values directly by name
    If(:Type = Char(Cats[i]), Insert Into(Spud, :COLUMN_NAME) ;
);

));

statement.


Slán



SpannerHead
3 REPLIES 3
jthi
Super User

Re: For Each Row with Insert Into

It is unnecessary to use For Each Row for this (if I understand correctly what you are going for). Just use For Each with << Get Rows Where and data table subscripting to get the values into your list.

-Jarmo
jthi
Super User

Re: For Each Row with Insert Into

The code provided cannot be run and is difficult to read as it lacks proper spacing. My guess is that it is AI created as it is using weird techniques for JSL (ordering keys of associative array even though they are already sorted and so on).

Just based on the code provided (and the question) I'm not sure what you are going for. But, you just might be replacing your Spud list each loop which is messing you up.

-Jarmo
Craige_Hales
Super User

Re: For Each Row with Insert Into

The consecutive assignments

Spud = Char(Cats(i));
Spud = {};

are overwriting the first with the second. They are also inside the outer loop, which is why @jthi suggests the list is being recreated rather than extended.

Adding a 

show( row(), spud, ...etc... );

statement several places to get intermediate results in the log can help too. Or 

write( "\!n x=" || char(x) || " y=" || char(y) );

if you want to format it. Or

write( Eval Insert("\!n x=^x^ y=^y^) );

which might be easier to read.

The JSL editor has a reformat command that can help when the loop indentation gets visually misaligned.

 

 

Craige

Recommended Articles