Geeks Talk

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.

variable, variable name

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 ...

Go Back   Geeks Talk > Web Development > PERL
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

PERL PERL Scripts - Tips, Tricks, Issues, Latest News, PERL Resource and PERL related topics can be discussed in this forum.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2009
Junior Member
 
Join Date: May 2008
Location: India
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
sajithkutty is on a distinguished road
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};
Thanks in advance.
Sajith
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-09-2009
Junior Member
 
Join Date: May 2008
Location: India
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
sajithkutty is on a distinguished road
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";
This would work, UNLESS one uses strict, which I am. Now my test code looks like this...

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";
  }
But I get the error
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
Reply With Quote
  #3 (permalink)  
Old 04-10-2009
Junior Member
 
Join Date: Apr 2009
Location: Ahmadabad, Gujarat
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
isha.dalwadi is on a distinguished road
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
Reply With Quote
  #4 (permalink)  
Old 04-27-2009
Junior Member
 
Join Date: May 2008
Location: India
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
sajithkutty is on a distinguished road
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
Reply With Quote
  #5 (permalink)  
Old 08-06-2009
Junior Member
 
Join Date: Aug 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
wecacuee is on a distinguished road
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";'
Reply With Quote
  #6 (permalink)  
Old 08-09-2009
Junior Member
 
Join Date: May 2008
Location: India
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
sajithkutty is on a distinguished road
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";
This worked. Just what I needed.
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
Reply With Quote
Reply

  Geeks Talk > Web Development > PERL

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 01:15 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved