Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on variable, variable name within the PERL forums, part of the Web Development category; Hi, I have this situation. I have a perl driver script that reads data from a data file. The data will be 1) function to call with arguments 2) error ...
|
|||||||
| PERL PERL Scripts - Tips, Tricks, Issues, Latest News, PERL Resource and PERL related topics can be discussed in this forum. |
![]() |
| LinkBack | Thread Tools | Display Modes |
|
|||
|
variable, variable name
Hi,
I have this situation. I have a perl driver script that reads data from a data file. The data will be 1) function to call with arguments 2) error message to print if the function call fails. My problem is, I need to be able to add variables in the messages in the data file. So my message could be something like, $MsgCount... Msg to print when function call fails. Where $MsgCount is calculated in my perl script before this message is read from file and printed. When the message is read from the file, the complete message string is saved to a variable (say $x). But when i print the value of $x in my perl script i need the value of $MsgCount printed instead of the literal string. I read somewhere i could something like ${$MsgCount} to accomplish this but it doesn't work. Could anyone fix my following program to cause the last print statement to ouput "value" please? Currently it prints out blank. Code:
$day1date = "value";
print "day1date=$day1date\n";
$temp = "\$day1date";
print "*****temp = $temp\n";
print ${$temp};
Sajith |
| Sponsored Links |
|
|||
|
Re: variable, variable name
OK answering one's own post is not fun.
Anyway.I got past the point where i was when i wrote my earlier post but now i'm stuck again. the code (from my previous post) should have been Code:
my $day1date = "value";
print "day1date=$day1date\n";
$temp = "day1date";
print "****temp = $temp\n";
print "----${$temp}\n";
Code:
#!/usr/bin/perl
use strict;
my $string = "value of var1 is \$variable1 and that of var2 is $variable2";
my $offset = 0;
my ($char, $variablename);
my $result=0;
my $startloc=0;
my $endloc=0;
my $lenght=0;
my $i = 1;
my $variable1 = "firstvalue";
my $variable2 = "secondvalue";
while ($endloc != -1) {
$char = '$';
$startloc = index($string, $char, $offset);
$offset = $startloc + 1;
$char = ' ';
$endloc = index($string, $char, $offset);
if ($endloc == -1) { $lenght = length($string) - $startloc; }
else { $lenght = $endloc - $startloc; }
$offset = $endloc + 1;
$variablename = substr ($string, $startloc, $lenght);
print "variablename = $variablename and its value is ${$variablename}.\n";
}
Can' use string("$variable1") as a SCALAR ref while "strict refs" in use at ..... i also tried no strict "refs"; just before print "variablename = $variablename and its value is ${$variablename}.\n"; but the output I get is variablename = $variable1 and its value is . variablename = $variable2 and its value is . I am hoping to get the following output variablename = $variable1 and its value is firstvalue. variablename = $variable2 and its value is secondvalue. Please help Thanks Sajith |
|
|||
|
Re: variable, variable name
Hey Sajith,
I am not sure about using the string as the variable name and getting the value. In your code i believe there is some mess happening with the references(addresses of the variables). But you can get the values of the variables using Hash with strict enable . Please see the below code. #!/usr/bin/perl use strict; my $string = "value of var1 is \$no1 and that of var2 is \$no2"; my $offset = 0; my ($char, $variablename); my $result=0; my $startloc=0; my $endloc=0; my $lenght=0; my $i = 1; my $no1 = "firstvalue"; my $no2 = "secondvalue"; my %hash; $hash{'$no1'} = $no1; $hash{'$no2'} = $no2; while ($endloc != -1) { $char = '$'; $startloc = index($string, $char, $offset); $offset = $startloc + 1; $char = ' '; $endloc = index($string, $char, $offset); if ($endloc == -1) { $lenght = length($string) - $startloc; } else { $lenght = $endloc - $startloc; } $offset = $endloc + 1; $variablename = substr ($string, $startloc, $lenght); #use Data: umper;#print Dumper{%hash}; print "variablename = $variablename and its value is $hash{$variablename}.\n"; } Output: D:\Perl_Scripts>perl temp.pl variablename = $no1 and its value is firstvalue. variablename = $no2 and its value is secondvalue. Let me know this helped you or not? ![]() Enjoy!! Regards, Isha |
|
|||
|
Re: variable, variable name
Hi Isha,
Thanks for your reply. THis script is quite close to what i actually need. Now my problem is in my actual script i will never know what variable forms a part of the string. So i cannot create hash references for the variables beforehand. I would also not like to limit the variables that can form a part of the string. Is there a way i can get rid of modify below statement such that it works for any variable in the complete script, not just $no1 and $no2. my %hash; $hash{'$no1'} = $no1; $hash{'$no2'} = $no2; Thanks again Sajith |
|
|||
|
Re: variable, variable name
I know its late but you can use PadWalker.
warning:"I wouldn't recommend using PadWalker directly in production code" Code:
perl -MData::Dumper -MPadWalker -e '
my $day1date = "1 June 2006";
print "$day1date=$day1date\n";
my $temp = "\$day1date";
my $pad = PadWalker::peek_my(0);
print "$temp=${$pad->{$temp}}\n";'
|
|
|||
|
Re: variable, variable name
Thanks wecacuee,
I tried your code and I got syntax error at line 1. I'm not sure if the single quotes at the end of 1st and last line are meant to be there. I tried couple of combinations but none worked. I finally tried Code:
use PadWalker;
use strict;
my $day1date = "1 June 2006";
print "day1date=$day1date\n";
my $temp = "\$day1date";
print "temp = $temp\n";
my $pad = PadWalker::peek_my(0);
print "$temp=${$pad->{$temp}}\n";
I already have my code working without strict. I'm not sure if I can have PadWalker installed on the UNIX environments I work on at my workplace. I'd love to include padwalker in my code, if I can. Why exactly do you say one shouldn't use it in prod? Thanks again. Sajith |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to use QTP for variable data | sangeetasingh268 | QTP | 3 | 02-25-2008 04:04 AM |
| Parameter variable and Mapping variable | kiranmanju | Data Warehousing | 0 | 01-08-2008 04:17 AM |
| Declare a variable in QTP | Geek_Guest | QTP | 2 | 11-22-2007 02:41 AM |
| Normal Variable and the Comp-3 Variable in Cobol | Geek_Guest | MainFrame | 0 | 07-04-2007 09:47 PM |
| redefine a variable | Geek_Guest | MainFrame | 1 | 05-18-2007 04:57 PM |