Re: define an array in perl



jeniffer wrote:
I have an array block_list :

push ( @block_list ,$word); # this word is read from a file.
$list_name = $block_list[$#block_list]; # i extract the last element
ie $word in this case
now i want to define an array with the name $list_name

like ,
my @"$list_name";

But this is giving me errors...

It can be done, but is not recommended. Consider this solution instead:

my $word = 'Perl';
my %lists; # declare a HoA

push @{ $lists{$word} }, $word;

print @{ $lists{Perl} }, "\n"; # prints 'Perl'

You may want to read the FAQ entry

perldoc -q "variable name"

about why what you tried to do is not recommended.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
.



Relevant Pages

  • Re: style of functions with complex arguments
    ... > where the list at the end is actually a set of 3 triples. ... triples I would use an array reference as well. ... then you need to check that the 'weights' is an array ref (perldoc -f ...
    (perl.beginners)
  • RE: Why doesnt this work?
    ... Any time you don't understand a Perl function, look it up via perldoc. ... the shiftin the sub: ... Removes the first element of an array and stores it in $search. ...
    (perl.beginners)
  • RE: How to store the out put in StringBuffer
    ... I have been mentioning @tail. ... An array is the one variable that can do what you ... perldoc perlintro ... How to store the out put in StringBuffer ...
    (perl.beginners)
  • Re: problem with eval
    ... What is in the @_ array? ... only subroutine arguments go to ... evaluates that string as *Perl* code. ... perldoc -f system ...
    (perl.beginners)
  • Re: Easy Field Grabbing Question
    ... > ok another stupid question time! ... > how does the above work for an array?? ... While, with perldoc, it can sometimes be a little hard to figure out how ... and learn some of the basics first. ...
    (comp.lang.perl.misc)