Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

define an array in perl

2 views
Skip to first unread message

jeniffer

unread,
Jul 31, 2007, 6:43:42 AM7/31/07
to
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
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...
sorry for the stupid question,,,please help me out ,,,,

anno...@radom.zrz.tu-berlin.de

unread,
Jul 31, 2007, 6:59:57 AM7/31/07
to
jeniffer <zenith.of....@gmail.com> 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

Gunnar Hjalmarsson

unread,
Jul 31, 2007, 6:59:34 AM7/31/07
to
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

ivak...@gmail.com

unread,
Jul 31, 2007, 7:03:45 AM7/31/07
to

arrays starts its name from @. you cant define an array with the name
$list_name, it looks like a string, member of array, but not the
array. You may want to split $list_name into array. So use split().
Example: my @arr=split('-',$list_name). It splits the string
$list_name into an array with '-' as a delimiter.
my $str="abc-def-123-456";
my @array=split('-',$str);

mattsteel

unread,
Aug 1, 2007, 3:51:17 AM8/1/07
to

This is an eval task: instead of
my @"$list_name";
I'd use
eval "my \@$list_name";

M.

David Formosa (aka ? the Platypus)

unread,
Aug 5, 2007, 12:42:46 AM8/5/07
to
On Wed, 01 Aug 2007 00:51:17 -0700, mattsteel <matteo_...@virgilio.it> wrote:

[...]

> This is an eval task: instead of
> my @"$list_name";
> I'd use
> eval "my \@$list_name";

That will not work. The scope of the my in that block is restricted
to within the eval.

0 new messages