CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
how to access the value of a variable like $value = 10

 



vishwa787
New User

Jul 29, 2010, 3:50 AM

Post #1 of 4 (1919 views)
how to access the value of a variable like $value = 10 Can't Post

Hi,

I need to create a script which can check if the values are present in a file.

The code is as follows:

$value = 10;

$string = "value";



Now i need to access the value of $value i.e 10 by $string. I am not talking here something about pointers or references.



The only thing i need is I will be passing strings to a function such as fun("value") etc. Inside this function will be set of predetermined values such as $value $weight $price etc.

I want to return the value of $value from the function for which i have passed value as a parameter

sub fun()

{
my($string) = @_;

$value = 10;

$price =290;



return(${$string})
}



Here I want to return 10 as the value. How do I map the $string to $value.

I tried many ways ${\"$string\"} but was unable to get the value as 10 in my output.



Any help would be appreciated


BillKSmith
Veteran

Jul 29, 2010, 4:51 AM

Post #2 of 4 (1917 views)
Re: [vishwa787] how to access the value of a variable like $value = 10 [In reply to] Can't Post

You need a hash.


Code
  

sub get_value {

my %values = (

value => 10,

weight => 3,

price => 290,

);

(my $string) = @_;

return $values{$string};

}



You don't really need the subroutine. You can put the hash in the main program. Access it just like the return.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Jul 29, 2010, 5:01 AM

Post #3 of 4 (1916 views)
Re: [vishwa787] how to access the value of a variable like $value = 10 [In reply to] Can't Post


Quote
Now i need to access the value of $value i.e 10 by $string. I am not talking here something about pointers or references.


Yes you are talking about references. To be exact, you're talking about symbolic references, which are really bad and should not be used.

Follow Bill's advice and use a hash.


vishwa787
New User

Jul 29, 2010, 6:51 AM

Post #4 of 4 (1907 views)
Re: [FishMonger] how to access the value of a variable like $value = 10 [In reply to] Can't Post

Thanks a lot for your suggetions

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives