Re: define an array in perl



jeniffer <zenith.of.perfection@xxxxxxxxx> wrote in comp.lang.perl.misc:
Hi
I am a newbie in perl. 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

Why don't you use $word itself? If you still have the variable, there's
no need to access the array. If $word is no longer available, the
last element of an array is better accessed as

$block_list[-1];

ie $word in this case
now i want to define an array with the name $list_name


like ,
my @"$list_name";

Bad plan. You're aiming for symbolic references. See "perldoc -q
'variable as a variable name'" for why this is not a good idea.

But this is giving me errors...
sorry for the stupid question,,,please help me out ,,,,

Use a "hash of arrays" %h where the keys are the prospective variable
names and the values are references to the arrays. The array for
$list_name would be

@{ $h{ $list_name} }

and the $n-th element of that array can be accessed through

$h{ $list_name}->[ $n]

See "perldoc perldsc" and "perldoc perlreftut" for more on that.

Anno
.



Relevant Pages

  • Re: delimited data into nested array
    ... > file) into Perl as a two dimensional nested array. ... not declared your variables and enabled strictures. ... Here you are using symbolic references, ...
    (comp.lang.perl.misc)
  • Re: naming a variable with the datum from another variable
    ... the number of columns varies. ... No need to use symbolic references for loops. ... proper data structure (e.g. an array of hashes) you just loop through the ...
    (comp.lang.perl.misc)
  • Re: Some confusion about references
    ... you were using symbolic references, please dont do that unless you do ... when you print an array, all elements in that array are concatenated ... Shouldnt $ranges now be a reference to an array? ...
    (comp.lang.perl.misc)
  • Re: define an array in perl
    ... I am a newbie in perl. ... I have an array block_list: ... my %lists; ...
    (perl.beginners)
  • Re: define an array in perl
    ... I am a newbie in perl. ... I have an array block_list: ... This is an eval task: ...
    (comp.lang.perl.misc)