Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Dynamic Variable Names?

by davidraske (Initiate)
on Jul 23, 2002 at 15:26 UTC ( [id://184448]=perlquestion: print w/replies, xml ) Need Help??

davidraske has asked for the wisdom of the Perl Monks concerning the following question:

I thought this was possible, but for some reason its not working...
I have a large form with many similarly names vars...all appended with + a trailing increasing int. $foo_2, $foo_3, $foo_4,... $foo_100 I'm trying to do something like this: my @fee; for (my $i=0; $i < $MAX_ROWS; $i++){ my $local_foo = $foo_$i; push @fee, $local_foo; }#end loop
But my syntax is off I guess...any ideas?

s/pre/code/ - dvergin 2002-07-23

Replies are listed 'Best First'.
Re: Dynamic Variable Names?
by dragonchild (Archbishop) on Jul 23, 2002 at 15:32 UTC
    To do exactly what you're trying to do, do something like:
    for my $i (0 .. $MAX_ROWS - 1) { push @fee, ${"foo_$i"}; }

    That said, DON'T DO IT THAT WAY.

    That is what's called a "soft reference". Since you're coming from the web, you should know it's a security leak. Taint-checking fails that. strict fails that. (You are using string and -T, right?)

    As for a better solution, you need to change your form. CGI should be able to handle arrays. (You are using CGI, right?)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Dynamic Variable Names?
by broquaint (Abbot) on Jul 23, 2002 at 15:37 UTC
    The syntax is like so
    my $local_foo = ${"foo_$i"}
    But symbolically referenced variables are generally a bad idea. A better option would be to use either a hash or an array (as would be fitting in your case) e.g
    my @fee; for (0 .. $MAX_ROWS){ push @fee, $foo[$_]; }
    or better yet
    my @fee = @foo[0 .. $MAX_ROWS];
    The last example is an array slice documented in the perldata manpage.
    HTH

    _________
    broquaint

Re: Dynamic Variable Names?
by erikharrison (Deacon) on Jul 23, 2002 at 15:35 UTC

    You can create variables dynamically, true. But that's not actually what you need here. When ever you have a series of scalars with trailing integers, use an array, and then you can access the array elements in your for loop. Also, using foreach is more idiomatic (which is a fancy word for "better written")

    @foo = ('foo0', 'foo1', 'foo2', 'foo3'); foreach $member (@foo) { push @fee, $member; }
    Cheers,
    Erik

    Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet

Re: Dynamic Variable Names?
by ignatz (Vicar) on Jul 23, 2002 at 17:14 UTC
Re: Dynamic Variable Names?
by RollyGuy (Chaplain) on Jul 23, 2002 at 15:34 UTC
    Try using eval. I've done this with function names before. Your code will look like:
    I'm trying to do something like this: my @fee; for (my $i=0; $i < $MAX_ROWS; $i++){ my $eval_str = "push @fee, \$foo_$i"; eval($eval_str); }#end loop

    Here I create the command I want to execute as a string and then evaluate it with eval.
    UPDATE: I just saw dragonchild's comments and realized that my method probably wouldn't be safe for web applications. The eval would be dangerous, so use at your own risk.
      Except from the fact the eval contains a syntax error (you'd need to escape the @), what makes you think the eval is unsafe? There's no data coming from the outside. The eval is as safe as any other code from the programmer.

      There's a lot that can be argued against "variable names inside variable names", but security is only an argument if you use data from the untrusted environment as variable names.

      Abigail

        ... but security is only an argument if you use data from the untrusted environment as variable names.

        Really?

        Whenever code does not behave as expected, there is a real possibility of an unexpected consequence that compromises security. Therefore any programming practice that leads to bugs is also a source of potential security flaws, even if it is not obvious how to get there.

Re: Dynamic Variable Names?
by bronto (Priest) on Jul 24, 2002 at 12:11 UTC

    A return question for you: what stops you from using an array and $foo[1]...$foo[100] instead of $foo_#?</code>

    If you could use an array, say @foo, the problem of putting everything in @fee disappears... what I can't really get is how and why your variables from the form are named that way...

    In any case, you probabily need to take a look at CGI.pm

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://184448]
Approved by dragonchild
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found