cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
lala
Level VIII

How to batch generate assigned memory variables from lists using JSL?

For example, given a list of text, assign values by adding the letter "r" before each element and in the order in which the elements are in the list.

nam = {"A","B","C","D","E","H"};

This results in the following variables and their assignments:

rA=1;
rB=2;
rC=3;
rD=4;
rE=5;
rH=6;

Thanks!

4 REPLIES 4
lala
Level VIII

Re: How to batch generate assigned memory variables from lists using JSL?

It's not work

nam = {"A", "B", "C", "D", "E", "H"};
For( i = 1, i <= N Items( nam ), i++,
	"r" || nam[i] = i
);
jthi
Super User

Re: How to batch generate assigned memory variables from lists using JSL?

One way:

Names Default To Here(1);

nam = {"A", "B", "C", "D", "E", "H"};
For(i = 1, i <= N Items(nam), i++,
	Eval(Parse("r" || nam[i] || "=" || Char(i) || ""))
);
Show(rA, rB, rC, rD, rE, rH);

But there are very little cases where I would do this. I would most likely always use Associative Arrays or lists to handle situations like this

nam = {"A", "B", "C", "D", "E", "H"};
vals = {1, 2, 3, 4, 5, 6};
Show(vals[Contains(nam, "D")]);
aa = Associative Array({"A", "B", "C", "D", "E", "H"},{1, 2, 3, 4, 5, 6});
Show(aa);
-Jarmo
Craige_Hales
Super User

Re: How to batch generate assigned memory variables from lists using JSL?

I did not know about As Scoped until just now, and @Wendy_Murphrey it could use better documentation, but it lets you access a namespace a bit like an associative array.

nam = {"A", "B", "C", "D", "E", "H"};
For( i = 1, i <= N Items( nam ), i++,
	As Scoped( Global, nam[i]) = i;
);

show(a,b,c,d,e,h);

a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
h = 6;

Even though the documentation doesn't say it, it can be on the left hand side of = and the 2nd parameter can be a string.

 

I agree, most times I'd prefer a real associative array over this technique because your code can break in a terrible way if the { list of variable names } happens to include a real variable in your code that you don't want overwritten:

The list holding "nam" just clobbered the variable 'nam' which held the list...The list holding "nam" just clobbered the variable 'nam' which held the list...

Craige
Craige_Hales
Super User

Re: How to batch generate assigned memory variables from lists using JSL?

A slight variation on building the AA using a matrix for the values:

nam = {"A", "B", "C", "D", "E", "H"};
myvars = Associative Array( nam, 1 :: N Items( nam ) );

Show( myvars["A"], myvars["H"] );

myvars["A"] = 1;
myvars["H"] = 6;

 

Craige