That post seems to be missing.
joyjoy 111 points 3 years ago

The answer is a map or dictionary.

path: 0 3956928, hotness: undefined, score: 111, children: 4
CompassRed 12 points 3 years ago

It depends. If the variable names are arbitrary, then a map is best. If the variable names are just x_1, x_2, x_3, ..., x_n, then a list or dynamic array would be more natural. If n is constant, then a vector or static array is even better.

path: 0 3956928 3970575, hotness: undefined, score: 12, children: 0
DahGangalang 9 points 3 years ago

I've just figured out linked lists and I want to call that the solution ¯_(ツ)_/¯

path: 0 3956928 3965102, hotness: undefined, score: 9, children: 2
joyjoy 18 points 3 years ago

When you get a new hammer...

path: 0 3956928 3965102 3969222, hotness: undefined, score: 18, children: 0
NightAuthor 9 points 3 years ago

I mean, I wouldn’t recommend it, but seems like a data structure that could work.

path: 0 3956928 3965102 3978021, hotness: undefined, score: 9, children: 0
fubarx 65 points 3 years ago

In python, 'eval()' is your friend.

/maliciouscompliance

path: 0 3957103, hotness: undefined, score: 65, children: 4
kate 37 points 3 years ago

locals()[“x”] = 1

path: 0 3957103 3957288, hotness: undefined, score: 37, children: 1
hoshikarakitaridia 3 points 3 years ago

Oh god I hate you so much for this. It's beautiful that it's possible but I also want you to know you're instigating cybercrimes.

path: 0 3957103 3957288 4467739, hotness: undefined, score: 3, children: 0
Turun 13 points 3 years ago

Nah, the locals() or globals() object is much better for this.

path: 0 3957103 3960051, hotness: undefined, score: 13, children: 0
palordrolap 7 points 3 years ago

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.)

path: 0 3957103 3964981, hotness: undefined, score: 7, children: 0
wethegreenpeople 59 points 3 years ago

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.

path: 0 3957961, hotness: undefined, score: 59, children: 12
Brainsploosh 35 points 3 years ago
path: 0 3957961 3958252, hotness: undefined, score: 35, children: 9
mumblerfish 31 points 3 years ago

Would one not just use a dict/hashmap with int keys labelling lengths and the list of strings as the values?

path: 0 3957961 3958252 3959104, hotness: undefined, score: 31, children: 6
Brainsploosh 31 points 3 years ago
path: 0 3957961 3958252 3959104 3959873, hotness: undefined, score: 31, children: 1
QuazarOmega 17 points 3 years ago

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

path: 0 3957961 3958252 3959104 3959873 3961450, hotness: undefined, score: 17, children: 0
Diplomjodler 5 points 3 years ago

Found the Python guy.

path: 0 3957961 3958252 3959104 3961681, hotness: undefined, score: 5, children: 3
Silinde 8 points 3 years ago

'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.

path: 0 3957961 3958252 3959104 3961681 3968594, hotness: undefined, score: 8, children: 0
mumblerfish 6 points 3 years ago

I did use 'list' and forgot it is called an 'array' or 'vector' in other languages. So sure, close enough :-)

path: 0 3957961 3958252 3959104 3961681 3962020, hotness: undefined, score: 6, children: 0
avonarret1 3 points 3 years ago

Because those are limited to Python? 😜

path: 0 3957961 3958252 3959104 3961681 3961913, hotness: undefined, score: 3, children: 0
wethegreenpeople 6 points 3 years ago

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."

path: 0 3957961 3958252 3969555, hotness: undefined, score: 6, children: 1
wewbull 0 points 3 years ago

Yep, what you failed to realise at the time is you've just invented a dynamic data structure like a list or a dictionary.

path: 0 3957961 3958252 3969555 3974732, hotness: undefined, score: 0, children: 0
CoderKat 13 points 3 years ago

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.

path: 0 3957961 3965192, hotness: undefined, score: 13, children: 0
RyeMan 10 points 3 years ago

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.

path: 0 3957961 3970655, hotness: undefined, score: 10, children: 0
xigoi 49 points 3 years ago

Found the creator of PHP

path: 0 3960446, hotness: undefined, score: 49, children: 4
bear_with_a_hammer 19 points 3 years ago
path: 0 3960446 3963629, hotness: undefined, score: 19, children: 1
crazyman 9 points 3 years ago

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

path: 0 3960446 3963629 3972854, hotness: undefined, score: 9, children: 0
GlitchyDigiBun 12 points 3 years ago

Piiiitchforks! Getcher pitchforks heeeere!

path: 0 3960446 3966811, hotness: undefined, score: 12, children: 0
iluminae 5 points 3 years ago

Every time we see this in our legacy code we yell out: dolla-dolla bills 'yall!

path: 0 3960446 3983379, hotness: undefined, score: 5, children: 0
fluxion 46 points 3 years ago

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.

path: 0 3957810, hotness: undefined, score: 46, children: 4
Zangoose 11 points 3 years ago

Or just use a dictionary/hashmap if it's all in the same program

path: 0 3957810 3959372, hotness: undefined, score: 11, children: 3
Bizarroland 14 points 3 years ago

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.

path: 0 3957810 3959372 3960592, hotness: undefined, score: 14, children: 2
Zangoose 6 points 3 years ago

JavaScript: They were so focused on whether or not they could that they forgot to consider whether or not they should

path: 0 3957810 3959372 3960592 3967198, hotness: undefined, score: 6, children: 0
Feathercrown 2 points 3 years ago

I once created a system to automatically load and execute arbitrary JS files and any exported functions in them as part of a server

path: 0 3957810 3959372 3960592 3976178, hotness: undefined, score: 2, children: 0
Ddhuud 36 points 3 years ago

I see you're not ready for the ways of the JavaScript.

path: 0 3957638, hotness: undefined, score: 36, children: 1
lowleveldata 11 points 3 years ago

Is anyone really?

path: 0 3957638 3961546, hotness: undefined, score: 11, children: 0
yogsototh 29 points 3 years ago

I think it should work in Clojure

(doseq [i (range 10)]
  `(def ~(str "var-" i) i))
path: 0 3959221, hotness: undefined, score: 29, children: 2
Azzu 22 points 3 years ago

Everything goes with macros :D

path: 0 3959221 3959350, hotness: undefined, score: 22, children: 1
avonarret1 8 points 3 years ago

That rhymes! :D

path: 0 3959221 3959350 3961822, hotness: undefined, score: 8, children: 0
Pxtl 23 points 3 years ago

The reason I hate HTML: I've seen smart, reasonable people do this with IDs, and I'm not 100% sure they're wrong.

path: 0 3967014, hotness: undefined, score: 23, children: 3
jvisick 32 points 3 years ago

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.

path: 0 3967014 3975559, hotness: undefined, score: 32, children: 1
Clent 4 points 3 years ago

Agreed.

Also, HTML is only meant to be read by a browser's interpreter which has no problem keeping track of variable names.

path: 0 3967014 3975559 3985597, hotness: undefined, score: 4, children: 0
isVeryLoud 5 points 3 years ago

So long as the ID never repeats, it's cool

path: 0 3967014 3987361, hotness: undefined, score: 5, children: 0
lorty 22 points 3 years ago

It would probably be way easier than expected to do so in javascript. It would also be awful.

path: 0 3972507, hotness: undefined, score: 22, children: 5
Feathercrown 17 points 3 years ago
for(var i=0; i \< numLoops; i++){
    globalThis['example'+i] = doStuff(i);
}
path: 0 3972507 3972972, hotness: undefined, score: 17, children: 4
pingveno 12 points 3 years ago
for(var i = 0; i < numLoops; i++) {
    eval("var_" + i + " = " + i + ";");
}
path: 0 3972507 3972972 3982482, hotness: undefined, score: 12, children: 3
Feathercrown 10 points 3 years ago

eval is a path to powers many would consider to be... unnatural

path: 0 3972507 3972972 3982482 3986797, hotness: undefined, score: 10, children: 2
BubblyMango 2 points 3 years ago

Is it possible to learn this power?

path: 0 3972507 3972972 3982482 3986797 3986825, hotness: undefined, score: 2, children: 1
pixeltree 20 points 3 years ago

I remember being new to programming and wondering that and then getting introduced to arrays. Matrixes blew my mind

path: 0 3957135, hotness: undefined, score: 20, children: 1
ChickenLadyLovesLife 3 points 3 years ago

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.

path: 0 3957135 3981971, hotness: undefined, score: 3, children: 0
Cargon 17 points 3 years ago

In R:

assign("x", value)
path: 0 3966511, hotness: undefined, score: 17, children: 2
canihasaccount 11 points 3 years ago

Within a loop could be:

for(i in 1:10){
     assign(paste0("listNum", i), list(i, someStringVector[i], i:(i+20), i*value))
}```

And you can also use get() in the same way to dynamically retrieve a variable. 

I've gone so far into coding debauchery that I've dynamically assigned variables from dynamically retrieved ones, and I've done so fairly frequently.
path: 0 3966511 3966874, hotness: undefined, score: 11, children: 1
Schroed4 7 points 3 years ago

Legit thank you.

I've wondered on the right way to do this in R.... Too many times.

path: 0 3966511 3966874 3968059, hotness: undefined, score: 7, children: 0
pastermil 13 points 3 years ago

USE ASSOCIATIVE ARRAY, OR SOME KIND OF MAPPING DATA STRUCTURE

path: 0 3986467, hotness: undefined, score: 13, children: 1
ultratiem 1 point 3 years ago

So is that a no?

path: 0 3986467 4086896, hotness: undefined, score: 1, children: 0
Bishma 13 points 3 years ago

PHP variable variables

path: 0 3957666, hotness: undefined, score: 13, children: 0
fosforus 12 points 3 years ago
path: 0 3987013, hotness: undefined, score: 12, children: 0
MinekPo1 12 points 3 years ago

I recently saw a SO answer suggesting to use PHP as an alternative to the c preprocessor.

path: 0 3961776, hotness: undefined, score: 12, children: 0
InvertedParallax 9 points 3 years ago

Array, and put the names or hash or id in either another array or the same array as a tuple.

path: 0 3957080, hotness: undefined, score: 9, children: 0
fishinthecalculator 6 points 3 years ago

Just use lisp

path: 0 3987788, hotness: undefined, score: 6, children: 0
Acters 2 points 3 years ago

So basically, it is just creating a custom database with logic/functions.

path: 0 4181763, hotness: undefined, score: 2, children: 0
ryn 1 point 3 years ago

macros?

path: 0 4416499, hotness: undefined, score: 1, children: 0
FMT99 -1 points 3 years ago
path: 0 3961663, hotness: undefined, score: -1, children: 0

thanks for using Leebra!

go to feed...