in reply to Re: Re: use simple scalar names to point to specific array elements
in thread use simple scalar names to point to specific array elements

Silly me. Typing something from memory when I know how bad my memory is. Here's a correct version (which was cheerfully misremembered from Damian Conway):

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use vars '@var'; my @array = qw( foo bar baz ); *var = alias_to(@array[1,2]); print Dumper \@var; $var[1] = 'silly me'; print Dumper \@array; sub alias_to { \@_ }

Cheers,
Ovid

New address of my CGI Course.

  • Comment on Re: Re: Re: use simple scalar names to point to specific array elements
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: Re: use simple scalar names to point to specific array elements
by sauoq (Abbot) on Dec 06, 2003 at 01:30 UTC

    That's better. (And aliasing a slice is a nice twist on the theme.) But the OP wanted to be able to use simple scalars. Here's a modification that will do the trick:

    #!/usr/bin/perl use warnings; use strict; use vars qw( $alias ); sub alias_to { \$_[0] } my @array = qw( foo bar baz ); *alias = alias_to( $array[1] ); print "alias: $alias\n"; $alias = 'QUUX'; print "array: @array\n";
    It's really nothing more than a convenience function to do exactly the same thing as I suggested in my original answer in this thread. Looking at it, I'm not even sure it's that convenient. I think *alias = \$array[1]; is perfectly clear, and it doesn't require me to go check what the alias_to() sub actually does.

    -sauoq
    "My two cents aren't worth a dime.";