The audacity to do such a thing…
Array, and put the names or hash or id in either another array or the same array as a tuple.
Sometimes you have to do something this. Like when working with a horribly designed legacy database that put property values in a child table and you need to map those to actual properties in your API model.
It would probably be way easier than expected to do so in javascript. It would also be awful.
for(var i=0; i \< numLoops; i++){ globalThis['example'+i] = doStuff(i); }
for(var i = 0; i < numLoops; i++) { eval("var_" + i + " = " + i + ";"); }
eval
is a path to powers many would consider to be… unnaturalIs it possible to learn this power?
Not from ES6
Found the creator of PHP
What are talking about? $$ is the way to go :D
I can’t remember the exact use case but I did submit a PR at a previous job that used $$ and not a single senior developer questioned it
Piiiitchforks! Getcher pitchforks heeeere!
Every time we see this in our legacy code we yell out: dolla-dolla bills 'yall!
macros?
The reason I hate HTML: I’ve seen smart, reasonable people do this with IDs, and I’m not 100% sure they’re wrong.
So long as the ID never repeats, it’s cool
That’s because it makes sense when dynamically creating HTML. HTML is not a programming language, it’s simply markup - so if you want to generate some block of HTML in a loop and later access that block of HTML in JS (e.g. to interact with the UI separate from creating it in the first place), it’s a completely reasonable thing to do.
Agreed.
Also, HTML is only meant to be read by a browser’s interpreter which has no problem keeping track of variable names.
I remember being new to programming and wondering that and then getting introduced to arrays. Matrixes blew my mind
I started coding with Visual Basic 3 which basically only had arrays for holding multiple items, so probably 50% of my time was spent writing code to delete items - you had to iterate through the remainder of the array copying values from x to x - 1 and then re-dimensioning the array. I remember having my mind blown when proper collections were introduced with VB4.
Too many people in this thread are trying to give a serious answer to this question.
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 just use a dictionary/hashmap if it’s all in the same program
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.
JavaScript: They were so focused on whether or not they could that they forgot to consider whether or not they should
I once created a system to automatically load and execute arbitrary JS files and any exported functions in them as part of a server
Just use lisp
PHP variable variables
USE ASSOCIATIVE ARRAY, OR SOME KIND OF MAPPING DATA STRUCTURE
So is that a no?
I see you’re not ready for the ways of the JavaScript.
Is anyone really?
I think it should work in Clojure
(doseq [i (range 10)] `(def ~(str "var-" i) i))
Everything goes with macros :D
That rhymes! :D
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.
I distinctly remember having the same experience. For some reason I believed dynamic variable naming was a good idea. What was I on??
In lower level languages like C/C++ the reason becomes much more apparent when you learn about memory allocation and management (as a bonus it also really helps to understand how OS’s handle memory). Dynamically declaring variables in a loop would mean you need to allocate a chunk of memory for each variable that’s generated on the fly, most of, if not all of the dynamically declared variables would not even use most of their allocated memory resulting in a ton of extra overhead and wasted space within memory. An array is usually the answer when someone asks how to dynamically define variables. With an array you allocate the space needed in memory and can iterate across it block by block resulting in more control and efficiency within your reserved memory block. Linked lists are also a fun thing to look into when you aren’t sure how big your array needs to be. It’s a hard question to answer in a 100 level class because the answer actually goes pretty deep into low level programming, operating system and hardware principles.
I remember wondering this when I was first trying to self learn. It’s because I needed a map (or list + struct or something) and was such a noob I didn’t know what maps were. Whatever material I was learning from wasn’t good enough, especially for winging things. Plus I was trying to learn C++ and maps aren’t quite so built into the language as they are with a better first language like Python.
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
Would one not just use a dict/hashmap with int keys labelling lengths and the list of strings as the values?
A programmer might, as trained/conditioned by the limits of programming languages.
A human would intuitively not, these are meaningless and/or convoluted concepts to the untrained human.
I like the implication that programmers aren’t humans, but a sort of alien being naturally apt at algorithmic thinking, while puny humans are an irrational species that needs to undergo training from the mighty race of programmers if they hope to get into the field brought us here by the aliens
Found the Python guy.
‘List’ is the correct abstract term for any data structure that holds a given number of values in an order, regardless of the implementation. So Python’s List, or C++'s Array or Vector, or a Linked List are all considered lists in the abstract sense.
I did use ‘list’ and forgot it is called an ‘array’ or ‘vector’ in other languages. So sure, close enough :-)
Because those are limited to Python? 😜
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.”
Yep, what you failed to realise at the time is you’ve just invented a dynamic data structure like a list or a dictionary.