How dare you ask this? Was it just to get into a programming memes page?
How dare you ask this? Was it just to get into a programming memes page?
Or sometimes even just an array. The first time I thought I wanted to do this was 2003 and I was writing a perl script, and I was trying to loop through some sort of array, and write the outputs of some calculations to $val0 $val1 and so on, and I was neck deep into some horrible dark constructs like ${“val” . $i} before I actually realized that I really just wanted an array, you know, like the one I was already using.
It took me forever to understand map.
In python, ‘eval()’ is your friend.
/maliciouscompliance
In c, nothing and nobody is your friend.
Source: me
In Perl, eval can do similar things, but symbolic references are "better" (I'm fairly sure it's where PHP got the idea, and the syntax, from.) e.g.
$foo = "bar";
$$foo = "potatoes"; # $$foo = access the variable named in $foo, i.e. $bar
print $bar; # prints potatoes
Reading other responses, it seems like Python's globals object is not entirely dissimilar, especially if you know how Perl deals with symbolic references under the hood.
But just because you can doesn't mean you should. If you use strict; in Perl, it will fail to compile most of this nonsense. Use a hash / associative array / dictionary / whatever your language (natural and/or programming) calls them instead.
And I'm pretty sure that even without strict, local variables can't be accessed at all the symbolic way, which is probably for the best. (NB: local is a subtle thing in Perl. By "local" here, I mean the so-called my variables that aren't accessible outside their scope. local variables are actually localised globals. Enjoy that thought.)
The answer is writing a program that writes the variables dynamically to a file and including that file into the source file that uses them.
No need to thank me folks I’m just trying to make the world a better place.
Or in JavaScript you can build a string that creates the name of a variable in a sub loop by concatenating ""variable"+$i" and passing that value into a variable that is then read as the name of an incremental variable that a value is then passed to.
This has the advantage of being both extremely unwieldy and highly inscrutable, and there's a small chance it will make your coworkers send you death threats in slack.
I distinctly remember asking this question during a 100 level programming class but I just can not remember why I’d ever want to do this?
What problem could I have possibly have been trying to solve where this would seem like the answer.
A common problem (before learning it is impossible/fraught with danger) is categorisation, like sorting of strings.
Say you have a text, and need to count words of different lengths.
One intuitive approach is to pass through it once and add each word to a list for the corresponding length, as well as making lists as needed. No 7 letter words, no 7-letter-word-list, even though there are longer words.
As humans we’re good at sorting things into an unknown number of categories, and we have to unlearn that for programming
A programmer might, as trained/conditioned by the limits of programming languages.
A human would intuitively not, these are meaningless concepts to the untrained human.
This makes a ton of sense and I think you probably solved this mystery for me.
“Oh I need to iterate over something, and keep track of new information as I do it, therefore I should be able to create ‘dynamic variables’ as I progress.”
I think it should work in Clojure
ˋˋclojure (doseq [i (range 10)] (def ~(str “var-” i) i))
ˋ`ˋ
In R:
assign("x", value)Within a loop would be:
for(i in 1:10){ assign(paste0("baseName", i), i*value) }``` And you can also use get() in the same way to dynamically retrieve a variable. I've gone so far into programming debauchery that I've dynamically assigned variables from dynamically retrieved ones, and I've done so fairly frequently.Legit thank you.
I’ve wondered on the right way to do this in R… Too many times.
Agreed.
Also, HTML is only meant to be read by a browser’s interpreter which has no problem keeping track of variable names.