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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print "dynamic" variable

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
This is best asked via an example:

$P1 = "HELLO";
$P2 = "WORLD";
for ($i=1; $i<=2; $i++) {
print "$P$i\n";
}

As you can see, the print line will not print out "HELLO" then "WORLD". What do I need to modify to make it do so?

Thanks in advance!
 
This probably isn't the right way.. :) but it's how I figured it out

$P1 = "HELLO";
$P2 = "WORLD";
for ($i=1; $i<=2; $i++) {
#print "$i\n";
print ${"P$i"} . "\n";
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Cool! Why does that work? (just wondering)
 
Well.. inside of the quotes you have a hard code P and then the $i get translated to 1 or 2. Then the ${} says to treat the whole thing as a variable (or so I think :) ) Someone smarter than me should explain it!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
This is someting you probably never want to do is a real perl program anayway:

Code:
$P1 = "HELLO";
$P2 = "WORLD";
for ($i=1; $i<=2; $i++) {
  print "$P$i\n";
}

The above is better done with a hash:

Code:
use strict;
use warnings;


my %P = (1 => "HELLO", 2 => "WORLD");
for (my $i=1; $i<=2; $i++) {
  print $P{$i},"\n";
}



The reason it works as Travis posted is because its a soft reference, which is not a good idea to use in real programs. Try this:

Code:
use strict;
use warnings;


my $P1 = "HELLO";
my $P2 = "WORLD";
for (my $i=1; $i<=2; $i++) {
  print ${"P$i"},"\n";
}

returns:

Can't use string ("P1") as a SCALAR ref while "strict refs" in use at script line 8.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kevin :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
There are some instances (in other languages which don't have the rich set of data structures supported by perl) where you'd need to do this. However, if you find yourself ever needing to do this in perl, you're doing it wrong. So for the example above with where the dynamic name is actually based on a number, you'd use an array
Perl:
my $p = qw{HELLO WORLD};
print $_, "\n" foreach (@p);
If you need to add an unknown number of dynamically named variables, use a hash as KevinADC has suggested.

Cue ishnid's famous treatise on the perils of soft references...


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]
 
Heh. It's not mine: I just link to it all the time. this is worth a read to help in understanding why soft references are almost always a bad idea.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top