Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > define an array in perl

Reply
Thread Tools

define an array in perl

 
 
jeniffer
Guest
Posts: n/a
 
      07-31-2007
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 ,,,,

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-31-2007
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
 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      07-31-2007
jeniffer <(E-Mail Removed)> 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
 
Reply With Quote
 
ivakras1@gmail.com
Guest
Posts: n/a
 
      07-31-2007
On 31 , 14:43, jeniffer <(E-Mail Removed)> wrote:
> 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 ,,,,


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);

 
Reply With Quote
 
mattsteel
Guest
Posts: n/a
 
      08-01-2007

> 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 ,,,,


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

M.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static (non-zero) initialization of an array of structs (array of length #define LEN) anon.asdf@gmail.com C Programming 23 08-28-2007 11:54 PM
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
#define _ and #define __ Brian Takita Ruby 0 01-23-2006 04:34 AM
how define array of array? Geoff Cox Javascript 5 09-14-2005 12:42 PM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM



Advertisments