gavla has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a field $x_point with the contents of "total[ 26]". So now I have to get the value of array field $total[ 26]. $result = ${$x_point}; does not work. I do not know how to get this working. Thanks

Replies are listed 'Best First'.
Re: indirect use of array field?
by almut (Canon) on Mar 03, 2010 at 22:55 UTC
    #!/usr/bin/perl $x_point = "total[26]"; $total[26] = "foo"; $result = eval "\$$x_point"; print "result=$result\n"; # foo

    but having to do this at all usually is an indication of a design problem in the first place :)

      thank you very much, now it works: when you store info, also have to get it back
      eval ( "\$$ui2$ui8 = d51s ( \$ui7, \$ui5, \$ui4 )" );
      this works to thanks
Re: indirect use of array field?
by ww (Archbishop) on Mar 03, 2010 at 22:29 UTC

    WHYT?1

    1"what have you tried?"

    Show us some code and tell us how it fails

      A peace of my perl creation:
      if ( $xkey eq 'ptot' ) { if ( ${ "x_$bid$skey" } ne '' ) { uitpak ( "x_$bid$ske +y", 't_', '', 'upsa06' ); } for ( 11 .. $p_ptot ) { if ( ${"p_etot$_"} eq '+' ) { $p_atot [ [ $_ ] & +#93; = '+'; } elsif ( ${"p_etot$_"} eq '-' ) { $p_atot [ [ $_ +] ] = ''; } if ( $p_atot [ ] $_] eq '+' ) { $ptot.= "atot [ +[ $_ ] ] " . ','; } } } $valn = ''; @a_opti = split ( ',', $$xkey ); foreach ( @a_opti ) { $valn.= $_ . '=' . e51s ( $ { "$up$_ +" }, '', 'upsa_00', '' ) . ','; }

      this last line failes the ${"$field"} when $field points to an array-field

      So...., not explaning everything but when the array @a_opti is filled with single fields as $fld1 or so then it works, on the other hand when the array is filled with an array item as $arr [ [ 26 ] ] or so then it fails.

      THE PROBLEM:
      I have an array filled with pointers to fields, when the field is an normal $field then it works, when it is an array field $arr [ [ number ] ] then it don't.

      it's a little part of my perl.pl of 150.000 bytes and is very complex (please don't ask me why). It's not my day; even the square brackets in this text does not.... [ :) ]