SPUG: Symbolic references ???

Yitzchak Scott-Thoennes sthoenna at efn.org
Wed Jan 25 14:09:43 PST 2006

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;
> 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?

Symbolic references always find global variables, not lexicals.  So,
your push is changing the @::ftrte array, etc.  That's just one of the
reasons they are not such a hot idea.

my @ofcrte;
my @pxrte;
my @acrte;
my @farte;
my @ftrte;

my %foorte = (
  ofcrte => \@ofcrte,
  pxrte => \@pxrte,
   acrte => \@acrte,
  farte => \@farte,
  ftrte => \@ftrte


push @{$foorte{$lc_table}}, ...

Better yet, junk the individual arrays altogether and just use a hash
of arrays.

More information about the spug-list mailing list