On 17/04/2011 19:07, Parag Kalra wrote:
Hi,
I am not sure if this can be done. But just asking it out of curiosity. I
have written this snippet.
use strict;
use warnings;
my $var = '$str abc_xyz';
my $str;
for(my $i=1;$i<= 5; $i++){
$str = $i;
my $line = 'Line: '.$var;
print "$line\n";
}
Currently it displays:
Line: $str abc_xyz
Line: $str abc_xyz
Line: $str abc_xyz
Line: $str abc_xyz
Line: $str abc_xyz
But can I make it output following:
Line: 1 abc_xyz
Line: 2 abc_xyz
Line: 3 abc_xyz
Line: 4 abc_xyz
Line: 5 abc_xyz
i.e. I want $str to be dynamically replaced with the value of number.
You could use a subroutine instead of a variable, as below.
Your variable names need to be more descriptive. I can't tell what their
purpose is so have used your names, but please fix them before you use
this code.
HTH,
Rob
use strict;
use warnings;
my $str;
sub var { "$str abc_xyz" }
for my $i (1 .. 5) {
$str = $i;
my $line = 'Line: ' . var;
print "$line\n";
}
**OUTPUT**
Line: 1 abc_xyz
Line: 2 abc_xyz
Line: 3 abc_xyz
Line: 4 abc_xyz
Line: 5 abc_xyz
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/