Welcome to WebmasterWorld Guest from 54.196.233.208

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Perl variable in a variable

     
3:45 pm on Dec 4, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Aug 29, 2003
posts:244
votes: 0


Hi guys,

I didn't find this in the Perl man.

How can I use a variable in a variable in Perl?

Example which works in PHP (and I'm looking for something similar in Perl):

$item=film;
$film=speed;

echo $$item will return "speed"

5:01 pm on Dec 4, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 0


You're talking about a reference.

my $film = "speed";
my $item = \$film.

That way $film and $item will return "speed". But in this instance would be pointless.

Do you mind me asking what you intend on using it for? There may be a better way.

7:02 am on Dec 5, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


yea, he probably should use a hash, but I guess it will depend on his requirements.
7:59 am on Dec 5, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Aug 29, 2003
posts:244
votes: 0


I will give some more info about my problem.

I have a large file with a list of variables (about 15,000) in this format:
$1111111 = x11111;
$1111112 = x11112;
$1111113 = x11113;
$1111114 = y11214;
$1111115 = x11583;
...

I've got to include the file with the variables in another Perl script. This Perl script opens another file and checks the content of this file line by line. The lines start with e.g. 11111111, 1111112, ... When the line starts with 1111111 I've got to show x11111 in the output.

8:11 am on Dec 5, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Aug 29, 2003
posts:244
votes: 0


Now I get this as an output for $1111111 --> "SCALAR(0x18301c8)"
9:34 am on Dec 5, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


What happens when you try $$1111111?
7:56 pm on Dec 5, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


I think that variables that start with numbers are reserved by perl, so $1111111 will be seen as $1 by perl, which is a read only variable. For example:



$1111111 = 'foo';
print $1111111;

will produce an error: Modification of a read-only value attempted at script line 1.

You may need to read that file as a text file and convert it into a hash if you can't change the naming scheme of the variables.

11:14 pm on Dec 5, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 12, 2005
posts:70
votes: 0


You may want to just read the file into a (really big) hash? Or a database perhaps... Something like this (untested!):

while ($row = <DATAFILE>) {
my ($n,$v) = split(/ = /,$row);
$data_hash{$n} = $v;
}

Then $data_hash{'\$1111111'} would have the value "x11111".

6:03 pm on Dec 6, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 0


I think that variables that start with numbers are reserved by perl

I agree. I always remember from my C/C++ days that you shouldn't start variable names with a number.

The hash method will work for you though, although there is one mistake, single quotes don't need escape characters....

Then $data_hash{'$1111111'} would have the value "x11111".

And you should define %data_hash beforehand.