creating variable names within a perl script

Discussion in 'Perl Misc' started by bdz, Jul 16, 2006.

  1. bdz

    bdz Guest

    This has been bothering me for years and I think my problem is I do not
    know the proper way to ask the question.

    Say I want to create an open ended set of variables - I do not know how
    many I will have when the script asks me to start entering them.

    so i can do something like this:

    while (-1){
    print "enter a variable: ";
    $var =<STDIN>;
    chomp($var);

    # what i want to do at this point is give this variable a name such
    # as $var1, then when the loop comes around again I would like
    # to be able to give the next variable a different name but since
    # I do not know how many are being entered i cannot create the
    # names ahead of time. is there anyway to create variable
    # names on the fly in perl ?

    exit if $var eq '-1
    }
    bdz, Jul 16, 2006
    #1
    1. Advertising

  2. bdz

    Dr.Ruud Guest

    bdz schreef:
    > This has been bothering me for years and I think my problem is I do
    > not know the proper way to ask the question.
    >
    > Say I want to create an open ended set of variables - I do not know
    > how many I will have when the script asks me to start entering them.
    >
    > so i can do something like this:
    >
    > while (-1){
    > print "enter a variable: ";
    > $var =<STDIN>;
    > chomp($var);
    >
    > # what i want to do at this point is give this variable a name such
    > # as $var1, then when the loop comes around again I would like
    > # to be able to give the next variable a different name but since
    > # I do not know how many are being entered i cannot create the
    > # names ahead of time. is there anyway to create variable
    > # names on the fly in perl ?
    >
    > exit if $var eq '-1
    > }


    Use an array? perldoc -f push

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Jul 16, 2006
    #2
    1. Advertising

  3. bdz wrote:
    > so i can do something like this:
    >
    > while (-1){
    > print "enter a variable: ";
    > $var =<STDIN>;
    > chomp($var);
    >
    > # what i want to do at this point is give this variable a name such
    > # as $var1, then when the loop comes around again I would like
    > # to be able to give the next variable a different name but since
    > # I do not know how many are being entered i cannot create the
    > # names ahead of time. is there anyway to create variable
    > # names on the fly in perl ?


    Technically this is possible (it is known as symbolic references), but it is
    is highly inadvisable. Check the FAQ
    perldoc -q "variable name"
    or the gazillions of previous postings on this topic for details.

    In your particular case why don't you simply use an array? You don't even
    have to keep track of the current index, because in
    $arr[@arr] = <STDIN>;
    @arr will be evaluated to the number of elements in the array which happens
    to be one larger than the index of the currently last element and thus
    exactly the index you need to place the next element.

    jue
    Jürgen Exner, Jul 16, 2006
    #3
  4. Jürgen Exner wrote:
    > bdz wrote:
    >>so i can do something like this:
    >>
    >>while (-1){
    >> print "enter a variable: ";
    >> $var =<STDIN>;
    >> chomp($var);
    >>
    >># what i want to do at this point is give this variable a name such
    >># as $var1, then when the loop comes around again I would like
    >># to be able to give the next variable a different name but since
    >># I do not know how many are being entered i cannot create the
    >># names ahead of time. is there anyway to create variable
    >># names on the fly in perl ?

    >
    > Technically this is possible (it is known as symbolic references), but it is
    > is highly inadvisable. Check the FAQ
    > perldoc -q "variable name"
    > or the gazillions of previous postings on this topic for details.
    >
    > In your particular case why don't you simply use an array? You don't even
    > have to keep track of the current index, because in
    > $arr[@arr] = <STDIN>;
    > @arr will be evaluated to the number of elements in the array which happens
    > to be one larger than the index of the currently last element and thus
    > exactly the index you need to place the next element.


    Or just use push:

    push @arr, scalar <STDIN>;




    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Jul 16, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,765
  2. wanwan
    Replies:
    3
    Views:
    414
    Alex Martelli
    Oct 14, 2005
  3. News123
    Replies:
    2
    Views:
    454
    John Machin
    Nov 26, 2008
  4. jay
    Replies:
    7
    Views:
    232
    Chad Perrin
    May 3, 2007
  5. Replies:
    4
    Views:
    285
    ccc31807
    Jul 29, 2011
Loading...

Share This Page