cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Sign-in to the JMP Community will be unavailable intermittently Dec. 6-7 due to a system update. Thank you for your understanding!
  • We’re retiring the File Exchange at the end of this year. The JMP Marketplace is now your destination for add-ins and extensions.
  • JMP 19 is here! Learn more about the new features.

Discussions

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

Create a list for each elements present in a list.

Dear All,

 

Trying to create a list for each items present in a list.

 

Already have,

MasterList = {"test1", "test2"};

 

Required,

Append the string "value" to each items present in the MasterList and have to make each item as an individual list.

 

test1value = {};

test2value = {};

 

Thanks, Alex.

1 ACCEPTED SOLUTION

Accepted Solutions
Georg
Level VII

Re: Create a list for each elements present in a list.

Great Idea of Mark. 

This may look as follows in your case. You can effectively manage many lists in an Associative Array.

 

Names Default To Here( 1 );

MasterList = {"test1", "test2"};

// many variables containing lists
For Each( {item}, Masterlist, Eval( Parse( Eval Insert( "^item^value= {};" ) ) ) );
Show( test1value, test2value );

// associative array containing lists
Master_AA = Associative Array();
For Each( {item, index}, Masterlist, Master_AA[Masterlist[index] || "value"] = {} );
Master_AA["test1value"] = {1, 2, 3}; For Each( {key}, Master_AA, Show( key, Master_AA[key] ) );
Georg

View solution in original post

6 REPLIES 6
Georg
Level VII

Re: Create a list for each elements present in a list.

Probably this helps

Names Default To Here( 1 );

MasterList = {"test1", "test2"};

For Each( {item}, Masterlist, Eval( Parse( Eval Insert( "^item^value= {};" ) ) ) );
Show( test1value, test2value );
Georg

Re: Create a list for each elements present in a list.

You might want to use Associative Arrays instead of Lists.

Georg
Level VII

Re: Create a list for each elements present in a list.

Great Idea of Mark. 

This may look as follows in your case. You can effectively manage many lists in an Associative Array.

 

Names Default To Here( 1 );

MasterList = {"test1", "test2"};

// many variables containing lists
For Each( {item}, Masterlist, Eval( Parse( Eval Insert( "^item^value= {};" ) ) ) );
Show( test1value, test2value );

// associative array containing lists
Master_AA = Associative Array();
For Each( {item, index}, Masterlist, Master_AA[Masterlist[index] || "value"] = {} );
Master_AA["test1value"] = {1, 2, 3}; For Each( {key}, Master_AA, Show( key, Master_AA[key] ) );
Georg
AlexR846
Level III

Re: Create a list for each elements present in a list.

Thank you Georg, & Mark !!

Both solutions did the job !!

pmroz
Super User

Re: Create a list for each elements present in a list.

Slightly different approach using an associative array:

Names Default To Here( 1 );

MasterList = {"test1", "test2"};

// associative array containing lists
Master_AA = Associative Array();
for (i = 1, i <= nitems(masterlist), i++,
	one_key = masterlist[i];
	master_aa[one_key] = {};
);

// Load the lists with some data
insertinto(master_aa["test1"], {1, 2, 3});
insertinto(master_aa["test2"], {4, 5, 6});

print(master_aa);

Output:

["test1" => {1, 2, 3}, "test2" => {4, 5, 6}]
ErraticAttack
Level VI

Re: Create a list for each elements present in a list.

Just a quick point -- using Eval( Parse( Eval Insert( ... ) ) ) can be somewhat cumbersome and slow (although the slowdown is only apparent with a great many calls).  You can avoid it by accessing the "here" namespace directly, as follows:

 

Names Default To Here( 1 );

MasterList = {"test1", "test2"};

here = Namespace( "here" );
// many variables containing lists
For Each( {item}, Masterlist, here[item || "value"] = {} );
Show( test1value, test2value );
Jordan

Recommended Articles