Re: define an array in perl
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxx
- Date: 31 Jul 2007 10:59:57 GMT
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
.
- References:
- define an array in perl
- From: jeniffer
- define an array in perl
- Prev by Date: Re: define an array in perl
- Next by Date: Re: define an array in perl
- Previous by thread: Re: define an array in perl
- Next by thread: Re: define an array in perl
- Index(es):
Relevant Pages
|
|