Ask Experts Questions for FREE Help !
Ask
    theterabyteboy's Avatar
    theterabyteboy Posts: 1, Reputation: 1
    New Member
     
    #1

    Jul 1, 2008, 05:03 PM
    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.
    derobert's Avatar
    derobert Posts: 34, Reputation: 2
    Junior Member
     
    #2

    Jul 5, 2008, 12:01 PM
    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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Commercial names to botanical names [ 1 Answers ]

Sirs, I want to know botanical names of all fruits,plants and flowers,particulerly all horticulture products and floriculture products.i.e. from commercial popular name to botanical names. Thank you and with regards. J.B.Gandhi

'too dynamic'? [ 2 Answers ]

I'm 21 and never had a serious boyfriend(I never really stayed with a guy for longer than a month and never had sex with anyone). I have always been a busy girl, never had that much free time, and it never bothered me before. But it does now, cause I'm in college and have more free time, and also...

Dynamic IP [ 1 Answers ]

Hi all the Experts,

Variable Overhead efficiency variable [ 1 Answers ]

Variable overhead is applied in the basis of standard direct labor-hours. If the direct labor efficiency variance is unfavorable, the variable overhead efficiency variance will be? a Favorable b unfavorable c zero d indeterminable

Dynamic jsp [ 2 Answers ]

Hi:) I am doing a project where I have set up a website. I have added info into mysql. How do I dynamically get info. I have set up a clothes website, and if the customer uses drop down menus and selects "skirts" and "black" for example, how do I dynamically add the "skirts" and "black"...


View more questions Search