Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have to name arrays according to scalar value, may I know how I can achieve this in perl

foreach $val(@array){ here I need to create an array with the name of $val like the below @$val = connection to database($val) which returns an array }
but I dont know hoe to create an array with $val as name. "@$val" doesn't seems right. Any help would be appreciated.

Thanks
George

Replies are listed 'Best First'.
Re: naming array from a scalar
by moritz (Cardinal) on Nov 09, 2009 at 23:56 UTC
    Instead of using variable variable names, you should use a hash instead. See perldata, perldsc and perlreftut for details.
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: naming array from a scalar
by colwellj (Monk) on Nov 10, 2009 at 00:02 UTC
    George,
    You can create a hash of arrays.
    That way your array is 'named' by hash
    However your foreach loop dosen't make much sense to me in this context. I think you need to declare $val perhaps?
    my %hash; foreach my $val(@array){ push @{$hash{$val}}, database_call($val) }
    To then print a particular named array
    foreach(@{$hash{$val}}){ print $_ } #to find a particular item in a particular array print $hash{$val}[$arrayid]
      Thanks much..That works for me.
      -George
Re: naming array from a scalar
by shmem (Chancellor) on Nov 10, 2009 at 00:06 UTC
    I have to name arrays according to scalar value

    No, you don't. You have a XY Problem: you want to store those arrays somewhere for later use (X), and you think it would be convenient to name the arrays after each $var in turn, so you ask about that (Y).

    But there is a better way to do what you want (X) - make a hash with keys of each $var content, whose value is the corresponding array. See perlref.

    my %db_connections; foreach $val(@array){ $db_connections{ $val} = [ connection_to_database( $val) ]; }

    You don't want to name arrays after a scalar value. Here's why.

Re: naming array from a scalar
by AnomalousMonk (Archbishop) on Nov 10, 2009 at 00:56 UTC
    What you are trying to do is to use Symbolic references (also called 'soft references'; see perlref). This is a Bad Thing and produces only package (i.e., global) variables, but for better or worse, here it is:
    >perl -wMstrict -MData::Dumper -le "my @array_names = qw(foo baz); for my $array_name (@array_names) { no strict 'refs'; @$array_name = ( reverse split '', $array_name ); } print Data::Dumper->Dump([\@::foo, \@::baz], [qw(*foo *baz)]); " Name "main::foo" used only once: possible typo at -e line 1. Name "main::baz" used only once: possible typo at -e line 1. @foo = ( 'o', 'o', 'f' ); @baz = ( 'z', 'a', 'b' );
    perlref and other documentation linked below explain why symbolic/soft references are Bad. (Don't worry about the  Name "what::ever" used only once... warnings; they'll be the least of your problems.)

    A far better approach is to use a hash with 'hard' references to anonymous arrays:

    >perl -wMstrict -MData::Dumper -le "my @key_names = qw(foo baz); my %hash; for my $key_name (@key_names) { $hash{$key_name} = [ reverse split '', $key_name ]; } print Data::Dumper->Dump([$hash{$_}], [qq{hash{$_}}]) for @key_names; " $hash{foo} = [ 'o', 'o', 'f' ]; $hash{baz} = [ 'z', 'a', 'b' ];
    Again, see perlref, perlreftut, perldsc and perllol for extensive info.