Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$variable associated with a string?

Status
Not open for further replies.

AMiSM

Technical User
Joined
Jan 26, 2006
Messages
128
Location
US

Hi, folks
Is it possible to define a variable in some way within Perl that it reflects the value of a second variable without having to redeclare the first?
Something like this, maybe:

$xyz = "variable is $abc.\n";
$abc = 5;
print $xyz;
$abc = 10;
print $xyz;

Output:

variable is 5.
variable is 10.

Is it a reference that I'm reaching for?
 
There are many ways of doing what your requesting. From the ex u've posted, it seems that ur variables are pre-declared. If so, store them in an array and print the array || assign them to a variable.

use strict;
use warnings;

Code:
@list = qw (1 2 3 4 5);
foreach (@list) {
$a = $_;
print "$a\n";
}
 
>> Is it possible to define a variable in some way within Perl that it reflects the value of a second variable without having to redeclare the first?

Not if you are reusing the same variable. $abc in your example is the same variable being reused. So you have to do something to the varaible if you want it to have a new value.
 
When the interpreter hits
Code:
$xyz = "variable is $abc.\n";
it interpolates the value of $abc into the string, setting $xyz to "variable is .\n" (because $abc is undefined at this point). Because you don't do anything to change the value of $xyz, it won't change. What you can do is:
Code:
$xyz = "variable is ";
$abc = 5;
print $xyz, $abc, ".\n";
$abc = 10;
print $xyz, $abc, ".\n";

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 

I understand that the code as presented will not work for what I explained. The question was whether there was a way of stating it so the variable in the string is a reference to another variable, similar to the -variable option in Tk, or something. I would like the $xyz in the code to be updated automatically.
 
here you go...
Code:
#!/usr/bin/perl

# Set Error Trapping
use CGI::Carp qw(fatalsToBrowser warningsToBrowser); 
use warnings;
use strict;
#use diagnostics;

# Print Header 
print "Content-type: text/html\n\n";

my $abc = "nothing";

my %xyz->{ 'abc' } = \$abc; 

print "abc = " . ${%xyz->{'abc'}} . "<br />";

$abc = "something";

print "abc = " . ${%xyz->{'abc'}} . "<br />";
 

exit();

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
or as a single scalar rather than a hash ref
Code:
#!/usr/bin/perl

# Set Error Trapping
use CGI::Carp qw(fatalsToBrowser warningsToBrowser); 
use warnings;
use strict;
#use diagnostics;

# Print Template 
print "Content-type: text/html\n\n";

my $abc = "nothing";

my $xyz = \$abc; 

print "abc = " . $$xyz . "<br />";

$abc = "something";

print "abc = " . $$xyz . "<br />";
 

exit();

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
In that last print statement, can you go

print "abc = $$xyz<br />";

...instead? I would imagine so, just making sure.
Is this a reference we are talking about here?
 
its better to concatinate the print statement to ensure correct handling of the var
Code:
print "abc = " . $$xyz . "<br />";
, yes this is a reference, we have made a variable = the reference of another, so when that var changes they are reflected back as the other is the reference not the content.

the way you originaly asked to make it $xyz = "abc = $abc";

is not possible to do, you are making $xyz a string, which is a static piece of text! and has no dynamic property.

I've come up with another way of also doing this so you don't have to keep typing the text and it can be changed at any time like so...
Code:
#!/usr/bin/perl

# Set Error Trapping
use CGI::Carp qw(fatalsToBrowser warningsToBrowser); 
use warnings;
use strict;
#use diagnostics;

# Print Template 
print "Content-type: text/html\n\n";

my $abc = "nothing";

my @xyz;

$xyz[0] = \$abc; 
$xyz[1] = "The variable is ";

print $xyz[1] . ${$xyz[0]} . "<br />";

$abc = "something";

print $xyz[1] . ${$xyz[0]} . "<br />";

exit();

then you only have to print the array parts not the text , have a play with the concepts and see if it will work out for what you need.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I like the array thing. Seems like it might have some use in AI.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top