cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Get the free JMP Student Edition for qualified students and instructors at degree granting institutions.
Choose Language Hide Translation Bar
View Original Published Thread

Slicing Associative Arrays?

hogi
Level XII

Fox matrix indexing, one can use 0 and get a full slice:

m = [ 1 2 , 3 4];
m[0,1]; // [1, 3]

 

 

For Data tables this works as well:

 

test = new table("test", add rows(10), new column("col1",Character, set each value({"A","B","C"}[random integer(3)])),new column("col2", set each value(random uniform(5))));
test[4,0]

 

For lists of lists, this is not possible:

 

list= {{"A", 3},{"B", 5}};
list[0,1]

Sure, there are many lists of lists where such slicing is not possible, like {{"A", 3},{"B"}}.

 

On the other hand, there ARE many lists of list that would allow it.

 

Where it gets really interesting: Associative arrays of associative arrays.
Like with lists, the concept of Associative Arrays is more flexible than what we can do with a matrix or data table - so slicing will not work for them. But like with lists, there ARE Associative arrays where slicing will work ...

Is there a function in JSL, something like myAssociativeArray[0, "weight"], which does this job?

Names Default to Here(1);
dt = Open( "$SAMPLE_DATA/Big Class Families.jmp" );
dt << Save( "$DOWNLOADS/test.json" );

// for sake of simplicity we accept to lose 1x ROBERT
myIndex=dt[0,"name"]

myAssociativeArray = Associative Array(myIndex,ParseJSON(Load text file ( "$DOWNLOADS/test.json")));
// cool, but not what we want
mapToWeight = Transform Each({{key, value}}, aa, value["weight"]);

SliceAssociativeArray= Function({aa, indexname},
	Transform Each({value}, aa << get values, value[indexname])
);

SliceAssociativeArray(myAssociativeArray ,"weight") //myAssociativeArray [0, "weight"] 

 

4 REPLIES 4
jthi
Super User


Re: Slicing Associative Arrays?

Different data structures have different uses, for associative array (I think) it means storing unique keys and getting the values for those keys fast (it stores key-value pairs). Transform Each is one good option to use, just change the output

 

weights = Transform Each({{key, value}}, aa, Output("List"), 
	value["weight"]
);

 

 

-Jarmo
hogi
Level XII


Re: Slicing Associative Arrays?

Another cool trick - very useful

 

From the useability, I think 

weights = Transform Each({{key, value}}, aa, Output("List"), 
	value["weight"]
);

and 

	Transform Each({value}, aa << get values, value["weight"])

from the original post are on a comparable value

 

... quite far from aa[0, "weight"]

 

Just imagine that you want to get all the  aa[0,"2024",0,0,"date"].

jthi
Super User


Re: Slicing Associative Arrays?

I'm not sure what this should "query" aa[0,"2024",0,0,"date"].If you only wish to get "0" indices, you could store your values to associative array in such a way, that your keys are column names and values are lists of the row values.

Names Default To Here(1); 

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

aa = Associative Array();
For Each({colname}, dt << Get Column Names("String"),
	aa[colname] = As List(Column(dt, colname) << get values);
);

Generally if I want to store my data in table like format which can be accessed like a table, I would just use (private) JMP data table.

-Jarmo
hogi
Level XII


Re: Slicing Associative Arrays?

good point : )