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.
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] ) );
Probably this helps
Names Default To Here( 1 );
MasterList = {"test1", "test2"};
For Each( {item}, Masterlist, Eval( Parse( Eval Insert( "^item^value= {};" ) ) ) );
Show( test1value, test2value );
You might want to use Associative Arrays instead of Lists.
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] ) );
Thank you Georg, & Mark !!
Both solutions did the job !!
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}]
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 );