Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Concatenation on Varibale Name

by dushyant (Acolyte)
on Oct 17, 2012 at 17:15 UTC ( #999578=perlquestion: print w/replies, xml ) Need Help??
dushyant has asked for the wisdom of the Perl Monks concerning the following question:

I want to do string concatenation on the left side of the assignment operator (=) to build variable name. Below concatenation is not working; but by looking at it, you can get the idea what I am trying do.

for ( $a = 1; $a <= 10; $a++) { $('var_' . $a) = 5 + $a ; }

Replies are listed 'Best First'.
Re: Concatenation on Variable Name
by toolic (Bishop) on Oct 17, 2012 at 17:21 UTC
    perldoc -q variable
    How can I use a variable as a variable name?

    Try a hash:

    use warnings; use strict; use Data::Dumper; $Data::Dumper::Sortkeys=1; my %var; for ($a = 1; $a <= 10; $a++) { $var{'var_' . $a} = 5 + $a; } print Dumper(\%var); __END__ $VAR1 = { 'var_1' => 6, 'var_10' => 15, 'var_2' => 7, 'var_3' => 8, 'var_4' => 9, 'var_5' => 10, 'var_6' => 11, 'var_7' => 12, 'var_8' => 13, 'var_9' => 14 };

      Thanks for the reply and link.

Re: Concatenation on Varibale Name
by davido (Archbishop) on Oct 17, 2012 at 17:42 UTC

    Making your code work is as easy as using curly braces instead of parenthesis:

    for( $a = 1; $a <= 10; $a++ ) { ${'var_' . $a} = 5 + $a; } print $var_1, "\n"; print $var_10, "\n";

    But this isn't really what you want.

    This is called a symbolic reference, and is outlawed by strict 'refs', because of how easy it is to make a grave mistake when mingling data with code in such ways. It could be that you have one of those truly rare situations where it's necessary, but if you don't (and if you have to ask how, you probably don't), it's best to use an array or a hash instead, or hard references, or a combination thereof.

    Please read the following articles:

    Perl allows you to do really dangerous (and possibly stupid) things if you want, because it doesn't want to get in the way of doing something really amazing when you need to be amazing. But usually you don't need to be amazing, and don't need the risk associated with such death-defying feats of amazingness. For the 99.9% of the time when you don't need to be a dare-devil, Perl provides you with hashes, hard references, and arrays. The global symbol table (the thing you're manipulating when you use a variable as a variable name) is, itself, a form of a hash. If a hash is good enough for Perl, it's probably good enough for your uses too. ...though in this case I'd be inclined to favor an array since you're just numbering your elements anyway.


Re: Concatenation on Varibale Name
by aitap (Curate) on Oct 17, 2012 at 17:59 UTC
    It looks like you need an array:
    my @var; for my $a (1..10) { $var[$a] = 5 + $a; }
    Arrays are sorted and consume less memory than hashes, but allow only integer indexes (instead of arbitrary strings as hash indexes).
    Sorry if my advice was wrong.
Re: Concatenation on Varibale Name
by betterworld (Curate) on Oct 17, 2012 at 17:42 UTC

    I agree with toolic. You should definitely move your bunch of variables into a data structure like a hash; and you should by no means follow any advice to make your original approach work by replacing the parentheses with curly braces and using no strict "refs";

Re: Concatenation on Varibale Name
by dushyant (Acolyte) on Oct 17, 2012 at 18:11 UTC

    Thank you for replies

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999578]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2018-08-21 03:51 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (197 votes). Check out past polls.