How to Create a Dynamic Variable and Assign Value to It

How to create a dynamic variable and assign value to it?

You can use bash's declare directive and indirection feature like this:

p_val="foo"
active_id=$p_val
declare "flag_$active_id"="100"

TESTING:

> set | grep flag
flag_foo=100

UPDATE:

p_val="foo"
active_id="$p_val"
v="flag_$active_id"
declare "$v"="100"

> echo "$v"
flag_foo
> echo "${!v}"
100

Usage in if condition:

if [ "${!v}" -ne 100 ]; then
echo "yes"
else
echo "no"
fi

# prints no

how to create variable names dynamically and assigning values in python?

As noted in RunOrVeith's answer, a Dict is probably the right way to do this in Python. In addition to eval, other (non-recommended) ways to do this including using exec or setting local variables as noted here

stri = "mat"

for i in range(1,3):
exec("_".join([stri, str(i)])+'=1')

or locals,

stri = "mat"

for i in range(1,3):
locals()["_".join([stri, str(i)])] = 1

This define mat_1 = 1 and mat_2 = 1 as required. Note a few changes to your example (str(i) and range(1,3) needed, for python3 at least).

How to assign values to dynamic const variable in C?

The purpose of const is largely to help programmers avoid modifying an object unintentionally. When you declare a with const int *a, then the type of *a is const int. In particular, it is const qualified, and this means it is not modifiable per C 2018 6.3.2.1 1:

… A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type,…

Using *a as the left operand of an assignment violates C 2018 6.5.16 2:

An assignment operator shall have a modifiable lvalue as its left operand.

Essentially, declaring a with const int *a says you do not want to modify *a.

If you want to dynamically allocate memory, assign it an initial value, and then use it as a const type, you can do this with two pointers:

int *p = malloc(sizeof *p);
if (!p)
{
fprintf(stderr, "Error, unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
*p = 77;
const int *a = p;

Then *p is a modifiable lvalue you can use to change the memory, and *a is a non-modifiable lvalue you could pass to another routine with the const qualifier to help avoid inadvertent changes to the memory. (It is not an absolute guarantee; the C standard allows code to convert a const int * to an int * and use it to modify the memory, as long as it does not point to an object that was originally defined with const.)

You could further reduce the opportunity for accidentally modifying the memory by reducing the scope of p:

const int *a;
{
int *p = malloc(sizeof *p);
if (!p)
{
fprintf(stderr, "Error, unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
*p = 77;
a = p;
}
// p is not in scope here, but a is.
printf("*a = %d.\n", *a);

However, then, when you pass the address to free, you need to use a cast to remove const, as in free((void *) a);.

Dynamically create variable and assign value to it

I think that what you want is to define an item-level variable inside ng-repeat.

You can set an item-level variable with ng-init. This variable will be created in the child scope created for each iteration of ng-repeat.

Then, you can toggle (or apply whatever logic) this variable with a button. Then, I suggest using ng-if instead of ng-show to prevent generating DOM elements for items that user did not want to see.

<div ng-repeat="outerItem in outerList" ng-init="show = false">
<button ng-click="show = !show">show/hide inner</button>
<div ng-if="show" ng-repeat="innerItem in innerList">
...
</div>
</div>

Assign value to Dynamic variable inside While Loop

Create simple array or better List<string> for that purpose like this:

List<string> Scripts = new List<string>();
Scripts.Add("firstScriptName");
Scripts.Add("secondScriptName");
Scripts.Add("thirdScripName");

and you can access it like Scripts[0] for firsScriptName or irritate through it with foreach(string s in Scripts) or do transform it to simple array like string[] arrayScripts = Scripts.ToArray()

Dynamically declare and assign variables in a loop

You could use exec, but it's highly not recommended see here:

l = [box1, box2, box3]
for value in l:
exec(f"self.{value} = Button()")

I would recommend you to just store it in a dictionary:

l = [box1, box2, box3]
self.variables = {}
for value in l:
self.variables[value] = Button()


Related Topics



Leave a reply



Submit