how to construct a variable, variable name ?

A

Alex Johnson

hi, i'm trying to call a variable where the variables name is
constructed using text and the value of another variable, the kind of
code that i'm trying to write would be something like this:

#!/usr/bin/perl -w

my $var1 = "three";
my $total = 1;

$var2 = '$var' . $total;
print "$var2";


but rather than printing literally '$var1' i want it to print the
contents of the variable $var1, so it should print 'three', i couldn't
find the answer in the perl books, probably because don't know the
keyword to look for, or else just one of those things that's so simple
it's assumed you know already, so any advice would be gratefully
received.

thanks in advance,

Alex.
 
G

Gunnar Hjalmarsson

Alex said:
hi, i'm trying to call a variable where the variables name is
constructed using text and the value of another variable,

Are you? As far as I can understand from your example code, you are
trying to call a variable whose _value_ is constructed using the
values of other variables.
the kind of
code that i'm trying to write would be something like this:

#!/usr/bin/perl -w

my $var1 = "three";
my $total = 1;

$var2 = '$var' . $total;
----------^----^
Those quote characters makes it print literally '$var'.
print "$var2";
--------^-----^
Those quote characters are redundant.
but rather than printing literally '$var1' i want it to print the
contents of the variable $var1, so it should print 'three',

Maybe this is what you mean:

#!/usr/bin/perl -w
use strict;
my $var1 = 'three';
my $total = 1;
my $var2 = $var1 . $total;
print $var2;

That prints 'three1'.

You may want to read about quoting in Perl at
http://www.perldoc.com/perl5.8.0/pod/perlintro.html
 
J

Jürgen Exner

Alex said:
hi, i'm trying to call a variable where the variables name is
constructed using text and the value of another variable,

This is known as 'symbolic references' and the FAQ describes why that is a
bad idea as well as what to do instead. Please see PerlFAQ7: "How can I use
a variable as a variable name?"
the kind of
code that i'm trying to write would be something like this:

#!/usr/bin/perl -w

my $var1 = "three";
my $total = 1;

$var2 = '$var' . $total;

However this looks more like you are trying to combine the _value_ of $var1
with $total and assign it to $var2. Just loose those quotes and it will
work.
print "$var2";

Useless use of quotes here, too. Just get rid of them (although they are not
causing any harm here).
but rather than printing literally '$var1' i want it to print the
contents of the variable $var1, so it should print 'three', i couldn't

No, you are looking for symbolic references, indeed. Don't do that. If you
really want to know why please check Google. This topic comes up at least
once or twice every week.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top