Ask Experts Questions for FREE Help!
  Advanced
Register  |  Log in  
   Ask    
 Answer  
  Help  

Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
Free Answers in 3 Easy Steps

Register Now
3 Steps

At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.

Home > Computers & Technology > Programming > Scripting > Perl   »   Dynamic Variable Names

 
Thread Tools Display Modes
Question
 
 
#1  
Old Jul 1, 2008, 05:03 PM
theterabyteboy
New Member
theterabyteboy is offline
 
Join Date: Jul 2008
Posts: 1
theterabyteboy See this member's comment history on his/her Profile page.
Dynamic Variable Names

I am wondering how to create a variable with a variable name in it.

Ultimately what I want to do is:

my $frequency1 = $frequency;
my $frequency2 = $frequency;
my $frequency3 = $frequency;

but the line my $frequency# = $frequency occurs inside of a loop and I can only
use this instruction once and want to set the value of the appropriate frequency variable.

i have a variable named $attempts and attempts holds the value 1, then 2, and then 3 on the third iteration of the loop

I am trying to use some sort of command like:

my $frequency{$attempts} = $frequency;

but this is not working, can someone please help? Thank you very much.

Reply With Quote
 
     

Answers
 
 
Old Jul 5, 2008, 12:01 PM   #2  
Junior Member
derobert is offline
 
Join Date: Oct 2007
Posts: 33
derobert See this member's comment history on his/her Profile page.
You can refer to non-my variables by name, the syntax looks like this:

$frequency2 = 10;
my $var_name = "frequency2";
print $$var_name, "\n";

but you probably don't want to. If you have a list of frequencies, you probably want to use an array. E.g.,

my @frequencies = (10, 20, 30);

for (my $i = 0; $i < 2; ++$i) {
print $frequencies[$i], "\n";
}

Or, you could use foreach, map, etc. to iterate over the array.
  Reply With Quote
 
     


Thread Tools
Display Modes

 
Similar Sponsors

Similar Threads
Question Asker Forum Answers Last Post
'too dynamic'? fisk Dating 2 Jan 5, 2008 09:50 PM
commercial names to botanical names jbg20072 Botany 0 Dec 6, 2007 06:36 AM
Dynamic IP Rithy69 Networking 1 Sep 14, 2007 08:44 AM
Variable Overhead efficency variable JMS327 Accounting 1 Jun 3, 2007 04:06 AM
dynamic jsp sarah11282 Utilities 2 Feb 24, 2006 01:25 AM




Copyright ©2003 - 2007, Ask Me Help Desk.
All times are GMT -8. The time now is 11:46 PM.