Evaluating nester variables

Discussion in 'Perl Misc' started by Geoff, Nov 13, 2003.

  1. Geoff

    Geoff Guest

    I have an array:
    @array = [\$a, \$b, \$c];

    How do I evaluate what $a is? I've tried:
    print $array[0];
    But this gives me "$a".

    I also tried using eval, but I must be using it wrong cause it doesn't
    seem to do anything.

    Thanks in advance.
    Geoff, Nov 13, 2003
    #1
    1. Advertising

  2. Geoff wrote:
    > I have an array:
    > @array = [\$a, \$b, \$c];


    It's an array with one element that consists of a reference to an
    anonymous array with three scalar references.

    > How do I evaluate what $a is?


    print ${$array[0][0]};

    > I've tried:
    > print $array[0];
    > But this gives me "$a".


    Sounds very strange. It should give you something like
    'ARRAY(0x15551bc)'. You'd better post some complete code that
    illustrates the behaviour you describe.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 13, 2003
    #2
    1. Advertising

  3. Geoff

    Matija Papec Guest

    X-Ftn-To: Geoff

    Geoff <> wrote:
    >I have an array:
    >@array = [\$a, \$b, \$c];
    >
    >How do I evaluate what $a is? I've tried:
    >print $array[0];
    >But this gives me "$a".


    Can you describe what do you want as a final result?
    I heavily /guess/ that you really want to,
    $aref = [$a, $b, $c];
    print $aref->[0];

    >I also tried using eval, but I must be using it wrong cause it doesn't
    >seem to do anything.


    Leave eval alone for now and go carefully trough perldoc perlref.



    --
    Matija
    Matija Papec, Nov 13, 2003
    #3
  4. Geoff

    Matija Papec Guest

    Matija Papec <> wrote:
    >Leave eval alone for now and go carefully trough perldoc perlref.


    There should be "through".



    --
    Matija
    Matija Papec, Nov 13, 2003
    #4
  5. Geoff

    Ben Morrow Guest

    Matija Papec <> wrote:
    > Geoff <> wrote:
    > >I have an array:
    > >@array = [\$a, \$b, \$c];
    > >
    > >How do I evaluate what $a is? I've tried:
    > >print $array[0];
    > >But this gives me "$a".

    >
    > Can you describe what do you want as a final result?
    > I heavily /guess/ that you really want to,
    > $aref = [$a, $b, $c];
    > print $aref->[0];


    Or perhaps simply

    my @array = ($a, $b, $c);
    print $array[0];

    or then again maybe

    my @array = (\$a, \$b, \$c);
    print ${$array[0]};

    Ben

    --
    Musica Dei donum optimi, trahit homines, trahit deos. |
    Musica truces molit animos, tristesque mentes erigit. |
    Musica vel ipsas arbores et horridas movet feras. |
    Ben Morrow, Nov 13, 2003
    #5
  6. Geoff

    Geoff Guest

    Alright, here's a code snippet:
    <CODE>
    @DB_FIELDS=(name phone etc);
    my $fields = join (', ', @DB_FIELDS);
    $i=0;

    while ($i < $loops){
    my $j=0;
    foreach $field (@DB_FIELDS) {
    @values[$j] = "\$" . $field . $i;
    $j++;
    }
    ....
    $values .= join (', ', @values);
    someFunction($db, $fields, $values);
    $i++
    }
    </CODE>

    So, $values should look like '$name0, $phone0, $etc0, $name1, $phone1,
    $etc1, $name2...'.

    This is used to build an SQL command. The command is:
    <CODE>
    my $insert_string = qq~INSERT INTO $db ($fields) VALUES ($values)~;
    my $sth = $dbh->do("$insert_string") or croak $dbh->errstr;
    </CODE>

    This loops several times depending on the number of inputs from the user.

    I'm doing this because I have no idea how many "$values" will be
    inserting into the database because it's coming from a dynamic HTML form
    that grows depending on the user's input.

    Gunnar Hjalmarsson wrote:

    > Geoff wrote:
    >
    >> I have an array:
    >> @array = [\$a, \$b, \$c];

    >
    >
    > It's an array with one element that consists of a reference to an
    > anonymous array with three scalar references.
    >
    >> How do I evaluate what $a is?

    >
    >
    > print ${$array[0][0]};
    >
    >> I've tried:
    >> print $array[0];
    >> But this gives me "$a".

    >
    >
    > Sounds very strange. It should give you something like
    > 'ARRAY(0x15551bc)'. You'd better post some complete code that
    > illustrates the behaviour you describe.
    >
    Geoff, Nov 13, 2003
    #6
  7. Geoff

    Uri Guttman Guest

    gack!! use dbi bindings. and regular loops will do. there is no reason
    for code generation there. at the worst you would need to generate some
    sql but (almost) never perl in a dbi situation. go back and rethink your
    design. your current code is nasty and brutish and way off base.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 13, 2003
    #7
  8. Please study and comply with the posting guidelines for this group:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    Geoff wrote:
    > Alright, here's a code snippet:


    A lot of things could be said about that code, but I'll try to limit
    myself to the initial problem.

    I suppose that it's the variable @values you are struggling with. Your
    code populates it with:

    @values = ('$name0', '$phone0', '$etc0');

    which is something quite different compared to

    @values = [\$name0, \$phone0, \$etc0];

    So now we know that you are not dealing with references at all.

    Your initial question was (the equivalent of): "How do I evaluate what
    $name0 is?". Now my question to you is: Do you anywhere in your code
    assign anything to the variable $name0? If you don't, there isn't much
    to evaluate, is there?

    As a sidenote, you would probably need symbolic references to assign
    anything to that and other similar variables, and that approach is not
    advisable.

    But that aside, assuming that you somewhere in your program has:

    $name0 = 'Some name';

    you should be able to access the content of $name0 by saying:

    print eval $values[0];

    But I think you have a lot of debugging to do.

    HTH

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 14, 2003
    #8
  9. Geoff

    Geoff Guest

    OK. Well, the string that comes from the HTML looks like so:
    action=add&name1=tom&phone1=123&name2=bob&phone2=456.... and so on.

    Why you ask? Because I have a block of input fields like:
    Name: |________________|
    Phone: |________________|

    If you click an "add" button, it adds a second block on input fields
    like so:
    Name: |________________|
    Phone: |________________|

    Name: |________________|
    Phone: |________________|
    ....

    If you enter data into the form, then click the add button, the
    Javascript needs to save the existing data into an array, so that when
    the form is redrawn, it populates the fileds you've already entered data
    into.

    The way I figured out to do this was to increment the variable names by
    1 so that the right data goes into the right fields.


    Uri Guttman wrote:

    >gack!! use dbi bindings. and regular loops will do. there is no reason
    >for code generation there. at the worst you would need to generate some
    >sql but (almost) never perl in a dbi situation. go back and rethink your
    >design. your current code is nasty and brutish and way off base.
    >
    >uri
    >
    >
    >
    Geoff, Nov 14, 2003
    #9
  10. Have you read the posting guidelines yet? Not complying with them will
    make people here ignore you.

    Geoff wrote:
    > You are correct, I am not dealing with references. The code should
    > generate this:
    > @values=($name0, $phone0, $name1, $phone1,... $namen, $phonen)
    >
    > $name0 get's set by the below function that reads all incoming CGI
    > variables and turns them into perl variables:
    > sub getCGIVariables {
    > my $qname = '';
    > my $qvalue = '';
    > while(($qname, $qvalue) = each %in) {
    > $$qname = $qvalue;
    > }
    > } # end
    >
    > I've tried using "print eval $values[0];", but that returns "$name0".


    Then it seems as if you need to do some debugging.

    I must say that creating all those scalar variables makes little sense
    to me. You really should take a step back, read some _good_ book about
    basic Perl programming, and reconsider the approach in your program.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 14, 2003
    #10
  11. [Please do not top post]
    [Please do not blindly fullquote]

    "Geoff" wrote :
    > You are correct, I am not dealing with references.


    Unfortunately you are, with symbolic references :-((

    > The code should generate this:

    [...]
    > $$qname = $qvalue;

    [...]

    Are you sure you know what you are doing and why you are using them?
    And what limitations and problems they have?

    I _strongly_ recommend you read the FAQ 'perldoc -q "variable name"'

    jue
    Jürgen Exner, Nov 14, 2003
    #11
  12. Geoff

    Uri Guttman Guest

    >>>>> "G" == Geoff <> writes:

    don't top post. see the posting guidelines (posted regularly here)

    G> OK. Well, the string that comes from the HTML looks like so:
    G> action=add&name1=tom&phone1=123&name2=bob&phone2=456.... and so on.

    G> Why you ask? Because I have a block of input fields like:
    G> Name: |________________|
    G> Phone: |________________|

    G> If you enter data into the form, then click the add button, the
    G> Javascript needs to save the existing data into an array, so that when
    G> the form is redrawn, it populates the fileds you've already entered
    G> data into.

    blech. that can be done server side with little effort. it is built in
    to cgi.pm (sticky values i think it is called).

    G> The way I figured out to do this was to increment the variable names
    G> by 1 so that the right data goes into the right fields.

    those aren't variable but form fields. and my comment was on the sql
    part more than the html stuff. if you have multiple common fields, then
    using incremental names in sql makes little sense as you can't
    (easily) add columns as needed. your whole design needs a big rethink
    before you get into deeper spaghetti trouble.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 14, 2003
    #12
    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. Hagay Lupesko

    Evaluating url trust level

    Hagay Lupesko, Oct 19, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    367
    Hagay Lupesko
    Oct 19, 2003
  2. microsoft.private.windows.netserver.setup

    Evaluating Formulas in ASP.NET

    microsoft.private.windows.netserver.setup, Mar 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    376
    Steve C. Orr [MVP, MCSD]
    Mar 4, 2004
  3. Chad E. Dollins
    Replies:
    3
    Views:
    651
    Kai-Uwe Bux
    Nov 8, 2005
  4. Replies:
    9
    Views:
    935
  5. Replies:
    2
    Views:
    101
Loading...

Share This Page