SPUG: Symbolic references ???

Colin Meyer cmeyer at helvella.org
Wed Jan 25 13:00:42 PST 2006


Hi Chuck,

On Wed, Jan 25, 2006 at 12:50:17PM -0800, Orr, Chuck  (NOC) wrote:
>  
> Hello,
>  
> I have a group of 5 arrays, named as follows:
>  
> my @ofcrte;
> my @pxrte;
> my @acrte;
> my @farte;
> my @ftrte;

Great variable names! ;-)

>  
> I would like to push something on to one of them per iteration of a
> loop, the pushee determined by a variable named $lc_table.
>  
> If I read pgs 16 & 17 of the leopard (advanced perl programming)
> correctly, I should be able to use a symbolic reference to push to the
> appropriate array depending on the contents of the $lc_table variable.
> I have turned strict refs off within the loop.
>  
> here is my push line:
>  
> push @$lc_table, "rep bla bla bla bla bla";
>  
> when I then print the above listed arrays, they have nothing in them.
> However, if I print "@$lc_table"; within the loop, there are elements in
> the array.  Any suggestions?

A typical approach to your problem would be to use one hash, with
keys of the same names as your old arrays, and values of arrayrefs.

  my %table; # this hash will hold the arrays

  for my $value_to_push ( function_that_generates_values() ) {
    push @{ $table{ $lc_table } }, $value_to_push;
  }

  # later, read out some values

  print "the last value of ofcrte is: $table{ ofcrte }[ -1 ] \n";

-Colin.


More information about the spug-list mailing list