Dynamic Variable Name in Loop

How do I create dynamic variable names inside a loop?

Use an array for this.

var markers = [];
for (var i = 0; i < coords.length; ++i) {
markers[i] = "some stuff";
}

R: Read dynamic variable names generated in for loop

You may use get to do this -

library(dplyr)

for (i in 1:nrow(LabviewFiles)){
assign(x = paste("P",i , "Onsets_PRH", sep = "_"), value = t(subset.data.frame(All_Phase, All_Phase$Phase==i) %>%
filter(CONDITIONS == "NULL_TRIAL",
MISC_REWARD == 1,
MISC_PASSIVE_FAILED == 1) %>%
select(Feedback_onset)))

assign(x = paste("P",i , "Durations_PRH", sep = "_"), value = t(rep(0.5, times = length(get(paste("P",i , "Onsets_PRH", sep = "_"))))))
}

Note that using assign and creating variables in global environment is discouraged. You may also read Why is using assign bad?

How do you create different variable names while in a loop?

Sure you can; it's called a dictionary:

d = {}
for x in range(1, 10):
d["string{0}".format(x)] = "Hello"
>>> d["string5"]
'Hello'
>>> d
{'string1': 'Hello',
'string2': 'Hello',
'string3': 'Hello',
'string4': 'Hello',
'string5': 'Hello',
'string6': 'Hello',
'string7': 'Hello',
'string8': 'Hello',
'string9': 'Hello'}

I said this somewhat tongue in check, but really the best way to associate one value with another value is a dictionary. That is what it was designed for!

Dynamic variable names to mutate variables in for-loop

As we are passing string, convert to symbol and evaluate (!!)

func <- function(i) {

mutate(df1, !!i := case_when(!is.na(!! rlang::ensym(i)) ~ as.character(!! rlang::ensym(i)),
is.na(!!rlang::ensym(i)) & var0 != '1' ~ '4444',
TRUE ~ '0'))
}

-testing

for(i in vars) {
df1 <- func(i)
}
df1
var0 var1 var2 var3
1 1 0 1 NA
2 2 1 4444 1
3 2 0 0 0
4 1 1 0 4444
5 1 0 1 1
6 2 4444 4444 NA
7 2 4444 4444 1

We may do this with across as well

df1 %>%
mutate(across(all_of(vars),
~ case_when(!is.na(.) ~ as.character(.),
is.na(.) & var0 != '1' ~ '4444', TRUE ~ '0')))
var0 var1 var2 var3
1 1 0 1 NA
2 2 1 4444 1
3 2 0 0 0
4 1 1 0 4444
5 1 0 1 1
6 2 4444 4444 NA
7 2 4444 4444 1

Dynamic Variable Name / Increment Variable Name

This is what is called a "variable variable."
A variable definition is comprised of two parts: the dollar sign, which tells the interpreter that it's a variable, and the variable name per se, which in short is a string; I'll call it "body" here.

So, if you have $var = 'my_other_var' and $my_other_var = 'hey', you can use the string "my_other_var" as the "body" of a the variable call.

Then echo $$var yields "hey".

Here with your example:

<?php

$var1 = 'A';
$var2 = 'B';
$var3 = 'C';
$var4 = 'D';

for ($i = 1; $i < 5; $i++) {

$varToEcho = "var$i"; // will become var1, var2, var3 and so on

echo $$varToEcho;

}

Dynamic variable name changing in a loop

If your bash is recent enough you can use namerefs:

function foo {
for (( i = 1; i <= 3; i++ )); do
local -n number="n$i"
number="$i"
done
printf '%s %s %s\n' "$n1" "$n2" "$n3"
}

Demo:

$ foo
1 2 3

If you want to export the nX variables to the global scope just add the g attribute to the local declaration:

local -ng number="n$i"

And then:

$ foo
1 2 3
$ echo "$n1 $n2 $n3"
1 2 3

Note that a bash array doesn't have a limit to its size (apart the physical limit of the available computer memory):

function foo {
local -ag n=()
for (( i = 1; i <= $1; i++ )); do
n+=("$i")
done
}

And:

$ foo 5
$ printf '%s\n' "${n[*]}"
1 2 3 4 5
$ for (( i=0; i<${#n[@]}; i++ )); do printf '%s ' "${n[i]}"; done; printf '\n'
1 2 3 4 5
$ for val in "${n[@]}"; do printf '%s ' "$val"; done; printf '\n'
1 2 3 4 5

We could even mix the two approaches and pass the name of the array as first argument:

function foo {
unset n
unset -n n
local -ng n=$1
for (( i = 1; i <= $2; i++ )); do
n+=("$i")
done
}

And:

$ foo myArray 7
$ printf '%s\n' "${myArray[*]}"
1 2 3 4 5 6 7

Dynamic variable in loop javascript

You don't want to create 9 variables. Trust me. You want to create an object.

var m = {};
for(var i=1; i<10; i++){
m[i] = "Something";
}

You can also create an array (m = []), but since you are starting at 1 and not 0, I'd suggest an object.

Javascript/React: how to create dynamic variable names for a hook in a loop?

Use an array instead:

const animations = Array.from(
{ length: 4 },
useAnimation
);

and then reference, eg, animations[0] instead of anim0.



Related Topics



Leave a reply



Submit