variable evaluation and printing

Discussion in 'Perl Misc' started by manunderstress, Mar 21, 2007.

  1. in the code below, why will it print (and add to the hash) $status but
    not $ifconfig? Also when $status = "Production"; it fails to print or
    add to hash as well.

    something about variable evaluation/interpretation i'm not
    understanding?

    #ifconfig command on a solaris box; outputs multiple lines of text
    my $ifconfig=`ifconfig -a`;
    my $status='Production';

    my @params = qw(status ifconfig);
    foreach $item (@params) {

    if (defined $item) {
    print "item name: $item item value:". ${$item}. "\n";
    $post_checks{"$item"} = "${$item}";
    }
    }
    manunderstress, Mar 21, 2007
    #1
    1. Advertising

  2. manunderstress wrote:
    > in the code below, why will it print (and add to the hash) $status but
    > not $ifconfig? Also when $status = "Production"; it fails to print or
    > add to hash as well.
    >
    > something about variable evaluation/interpretation i'm not
    > understanding?
    >
    > #ifconfig command on a solaris box; outputs multiple lines of text
    > my $ifconfig=`ifconfig -a`;
    > my $status='Production';
    >
    > my @params = qw(status ifconfig);
    > foreach $item (@params) {
    >
    > if (defined $item) {
    > print "item name: $item item value:". ${$item}. "\n";
    > $post_checks{"$item"} = "${$item}";
    > }
    > }



    It looks like you want to do this instead:


    my %post_checks = (
    status => 'Production',
    ifconfig => scalar `ifconfig -a`,
    );



    (The answer to your question is in: perldoc -q "How can I use a variable as a
    variable name"; but don't do that.)



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Mar 21, 2007
    #2
    1. Advertising

  3. >
    > You are trying to use symbolic references, and have demonstrated one
    > reason they are not recommended (they only work on global variables and
    > your variables are lexical).
    >
    > See 'perldoc -q variable
    >
    > How can I use a variable as a variable name?
    >
    > for other reasons why not to do this. Use a hash, instead.
    >


    Fair enough, but now I'm at a loss how else to do what I want to do,
    that is, without using scalar variables I have previously created in
    the script as keys for the hash (%post_checks)- and their evaluation
    as the value for the hash.

    I have a bunch of scalar variables that will be dynamically generated
    (hostid, ifconfig, whatever) and I want to add each key->value pair -
    if it exists- to a hash (what I thought I was doing...by adding key-
    >values to %post_checks).
    manunderstress, Mar 22, 2007
    #3
  4. manunderstress

    Paul Lalli Guest

    On Mar 22, 12:56 pm, "manunderstress" <> wrote:
    > > You are trying to use symbolic references, and have demonstrated one
    > > reason they are not recommended (they only work on global variables and
    > > your variables are lexical).

    >
    > > See 'perldoc -q variable

    >
    > > How can I use a variable as a variable name?

    >
    > > for other reasons why not to do this. Use a hash, instead.

    >
    > Fair enough, but now I'm at a loss how else to do what I want to do,
    > that is, without using scalar variables I have previously created in
    > the script as keys for the hash (%post_checks)- and their evaluation
    > as the value for the hash.
    >
    > I have a bunch of scalar variables that will be dynamically generated
    > (hostid, ifconfig, whatever) and I want to add each key->value pair -
    > if it exists- to a hash (what I thought I was doing...by adding key->values to %post_checks).


    Don't dynamically generate scalar values. Dynamically generate the
    key/value pairs.

    You're not showing us how you're "dynamically generating" the scalar
    values, so we can't help you fix that part of your program. . .

    Paul Lalli
    Paul Lalli, Mar 22, 2007
    #4
  5. On Mar 22, 4:56 pm, "manunderstress" <> wrote:
    > > You are trying to use symbolic references,
    > > and have demonstrated one
    > > reason they are not recommended (they only work on global
    > > variables and your variables are lexical).

    >
    > > See 'perldoc -q variable

    >
    > > How can I use a variable as a variable name?

    >
    > > for other reasons why not to do this. Use a hash, instead.

    >
    > Fair enough, but now I'm at a loss how else to do what I want to do,
    > that is, without using scalar variables I have previously created in
    > the script as keys for the hash (%post_checks)- and their evaluation
    > as the value for the hash.
    >
    > I have a bunch of scalar variables that will be dynamically generated
    > (hostid, ifconfig, whatever) and I want to add each key->value pair -
    > if it exists- to a hash (what I thought I was doing...by adding key-
    > values to %post_checks).


    And therein is the problem. Why is it that you are attached to the
    idea that you want "a bunch of [named] scalar variables that will be
    dynamically generated"? This is precisely what people here (and the
    FAQ) are advising against (at least as a general rule). The way to
    have a "bunch" of scalars in Perl is (in general) to make them into a
    hash in the first place.

    If you are really determined to go with "a bunch of [named] scalar
    variables that will be dynamically generated" then you have no choice
    but to use symrefs (or their even more evil cousin, eval()).

    Can you perhaps explain what it is that you expect to gain from using
    "a bunch of [named] scalar variables" rather than a hash?
    Brian McCauley, Mar 22, 2007
    #5
    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. Ilias Lazaridis
    Replies:
    2
    Views:
    375
    Ilias Lazaridis
    Apr 24, 2005
  2. Ilias Lazaridis
    Replies:
    74
    Views:
    701
    Ilias Lazaridis
    Apr 4, 2005
  3. Ilias Lazaridis
    Replies:
    18
    Views:
    311
    Bill Guindon
    Apr 9, 2005
  4. mike leonard
    Replies:
    4
    Views:
    133
    Ravil Bayramgalin
    Mar 21, 2008
  5. Gelonida N

    lazy evaluation of a variable

    Gelonida N, Jun 17, 2012, in forum: Python
    Replies:
    0
    Views:
    206
    Gelonida N
    Jun 17, 2012
Loading...

Share This Page