homepage Welcome to WebmasterWorld Guest from 54.167.41.199
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Perl variable in a variable
turbohost




msg:3520187
 3:45 pm on Dec 4, 2007 (gmt 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"

 

Dabrowski




msg:3520257
 5:01 pm on Dec 4, 2007 (gmt 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.

perl_diver




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

yea, he probably should use a hash, but I guess it will depend on his requirements.

turbohost




msg:3520724
 7:59 am on Dec 5, 2007 (gmt 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.

turbohost




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

Now I get this as an output for $1111111 --> "SCALAR(0x18301c8)"

rocknbil




msg:3520753
 9:34 am on Dec 5, 2007 (gmt 0)

What happens when you try $$1111111?

perl_diver




msg:3521154
 7:56 pm on Dec 5, 2007 (gmt 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.

adwatson




msg:3521266
 11:14 pm on Dec 5, 2007 (gmt 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".

Dabrowski




msg:3521799
 6:03 pm on Dec 6, 2007 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved