CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Got a variable name as string, want to set the variable

 



Henning Koch
Deleted

Aug 28, 2000, 12:02 PM

Post #1 of 3 (137 views)
Got a variable name as string, want to set the variable Can't Post

$abc = '$def';

I now want to change the value of $def, but without refering to the $def (which could be any other scalar name) directly - all I know is that there's a scalar name stored as string in $abc.

In non-working pseudo code I want to do this:

eval($abc) = 'new value';

Any ideas?

Henning


japhy
Enthusiast

Aug 28, 2000, 3:01 PM

Post #2 of 3 (137 views)
Re: Got a variable name as string, want to set the variable [In reply to] Can't Post

You're very close:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


eval "$abc = 'newvalue'";
</pre><HR></BLOCKQUOTE>

Perl first interpolates $abc's value in the double quoted string, and then executes the code that results:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$def = 'newvalue'
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



Henning Koch
Deleted

Aug 29, 2000, 12:35 AM

Post #3 of 3 (137 views)
Re: Got a variable name as string, want to set the variable [In reply to] Can't Post

Thanks a lot!

Henning

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives