The audacity to do such a thing…

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

@fosforus@sopuli.xyz
link
fedilink
12
edit-2
1Y

Also known as “my programming language doesn’t allow that therefore what you’re trying to do is a stupid idea”

(could just be a stupid idea too, though)

USE ASSOCIATIVE ARRAY, OR SOME KIND OF MAPPING DATA STRUCTURE

In R:

assign("x", value)

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.

Legit thank you.

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

@lorty@lemmy.ml
cake
link
fedilink
231Y

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);
}
@pingveno@lemmy.ml
link
fedilink
12
edit-2
1Y
for(var i = 0; i < numLoops; i++) {
    eval("var_" + i + " = " + i + ";");
}

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

Is it possible to learn this power?

Not from ES6

Pxtl
link
fedilink
English
231Y

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

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.

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

unalivejoy
link
fedilink
English
681Y

The answer is a map or dictionary.

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 (the metafunction).

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.

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

unalivejoy
link
fedilink
English
191Y

When you get a new hammer…

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

In python, ‘eval()’ is your friend.

/maliciouscompliance

Turun
link
fedilink
111Y

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

palordrolap
link
fedilink
71Y

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

kate
link
fedilink
English
201Y

locals()[“x”] = 1

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.

In c, nothing and nobody is your friend.

Source: me

++ is your friend

Fonzie!
link
fedilink
41Y

Objectively…

@30p87@feddit.de
link
fedilink
4
edit-2
1Y

# is your worst enemy despite being a copy of a good language

👍Maximum Derek👍
link
fedilink
English
31Y

PHP variable variables

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

Kit Sorens
link
fedilink
English
121Y

Piiiitchforks! Getcher pitchforks heeeere!

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

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.

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.

@CoderKat@lemm.ee
link
fedilink
English
141Y

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.

apotheotic (she/her)
link
fedilink
English
41Y

I distinctly remember having the same experience. For some reason I believed dynamic variable naming was a good idea. What was I on??

Too many people in this thread are trying to give a serious answer to this question.

@dark_stang@beehaw.org
link
fedilink
English
21Y

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.

macros?

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

Create a post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
  • 1 user online
  • 120 users / day
  • 257 users / week
  • 744 users / month
  • 3.72K users / 6 months
  • 1 subscriber
  • 1.48K Posts
  • 32.6K Comments
  • Modlog