cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Register to see how to import and prepare Excel data on Jan. 30 from 2 to 3 p.m. ET.

Discussions

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

How to get a table (small) into a list of Associatives Arrays OR how to script column name inside "For Each Row"

Hi all!

 

I need to get a small table into a list of Associative Arrays. Where each row is a member of that list and is an Associative Array with column names as keys and row values as values.

Currently I have this:

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
listAA = {};
columnList = dt << Get Column Names( string );
For Each Row(
	currentAA = Associative Array();
	For Each( {columnName, index}, columnList, currentAA[columnName] = Column( dt, columnName ) );
	Insert Into( listAA, currentAA );
);

//Check
listAA[1]["age"]


//:*/
listAA[1]["age"]
/*:

Column( "age" )

The output is not a value, but a reference to a column. So looks like it doesn't work when you refer to column using Column(name) inside For Each Row, only hardcoded names like in the Scripting Index:

For Each Row( :height = -:height );

Questions:

 

1. Is there a simple elegant way to get a list of AAs from a table? If not - how would I need to change my current version?

2. How to refer to a column not in a hardcoded way when using "For Each Row"?

 

Thanks, 

M.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to get a table (small) into a list of Associatives Arrays OR how to script column name inside "For Each Row"

Maybe something like this 

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

collist = dt << Get Column Names(string);
l = {}; For Each Row( aa = Associative Array(collist, dt[Row(), 0]); Insert Into(l, aa); ); Show(l);
-Jarmo

View solution in original post

2 REPLIES 2
jthi
Super User

Re: How to get a table (small) into a list of Associatives Arrays OR how to script column name inside "For Each Row"

Maybe something like this 

Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

collist = dt << Get Column Names(string);
l = {}; For Each Row( aa = Associative Array(collist, dt[Row(), 0]); Insert Into(l, aa); ); Show(l);
-Jarmo
jthi
Super User

Re: How to get a table (small) into a list of Associatives Arrays OR how to script column name inside "For Each Row"

You can also save it as .json and then parse it into AA

Names Default To Here(1);

path = "$TEMP/test.json";

dt = Open("$SAMPLE_DATA/Big Class.jmp");

dt << Save(path);
json_str = Load Text File(path);
Delete File(path);

l = Parse JSON(json_str);
-Jarmo

Recommended Articles