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

Hey guys, New perl user here. I'm getting an error and I'm guessing i'm just stupid. I tried searching a bit and really didn't find a solution Here's some silly test code I'm using. I have an array of array names (actually an array of strings). I simply want to grab that array of strings and use it to create arrays of the same name. I've got to do something like this on a much larger scale, but decided to do proof of concept first. Here's my test code snippet (I get no syntax or compilation error but it doesn't work)
#!/usr/bin/env perl use Cwd; our $cwd = getcwd(); @arr = ('cars', 'trucks'); @$arr[0] = (4,3,2,1); print @trucks;
This on the other hand, works quite well and it seems to be the same thing with one line condensed
#!/usr/bin/env perl use Cwd; our $cwd = getcwd(); @arr = ('cars', 'trucks'); $str = $arr[0]; @$str = (4,3,2,1); print @cars;
Wondering if somebody could point me in the right direction or slap some sense into my head Thanks in advance

Replies are listed 'Best First'.
Re: Working with an Array of Array Names
by ikegami (Patriarch) on Oct 07, 2007 at 23:23 UTC
Re: Working with an Array of Array Names
by snopal (Pilgrim) on Oct 07, 2007 at 23:28 UTC

    If you want to name arrays, your best bet is to use a hash to an array ref. With this concept you can do this:

    @{$type{'cars'}} = (1,2,3,4); @{$type('trucks'}} = (4,5,6,7);

    Not only is this easier to read, but it is much more flexible to expansion and for others to maintain.

    By flexible, I mean you are removing the requirement to hard code array names later in your code. You can easily access the array by the type name where ever it is needed. Also, because some types may be similar, it promotes code re-use for your stored values.

    The only reason I can see to do it as you are outlining is because you are prematurely optimizing your array use. Generally, you should only optimize for speed when it is determined that a certain structure or use is a detriment to the process. Yes, hashes are more expensive, but so is follow-up maintenance.

Re: Working with an Array of Array Names
by bruceb3 (Pilgrim) on Oct 07, 2007 at 23:38 UTC
    Fist of all, I am not sure exactly what you are trying to do, so here is an answer with my best guess at what you are wanting to achieve.

    You really need to read perlref. If you are using a *NIX server, from the command line type;

    perldoc perlref

    And study away. Now to my "solution";

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $arr; push @{$arr->{car}}, ( 4,3,2,1 ); print Dumper $arr;

    Some explaination; The $arr->{car} is a reference to a hash where the key is the string "car". The @{ } says to treat the value of the hash as an array. And you know what push does. perlref will be explain it completely.

      Thanks a lot guys, and thanks for the reading material. Like I said, I'm a complete scripting noob, but I've somehow managed to get a lot done by goggling and reading through some stuff on this and other forum. From what I've seen so far, there's a lot of shorthand, and too much I don't yet know. Thanks for your help.
Re: Working with an Array of Array Names
by cosmicperl (Chaplain) on Oct 08, 2007 at 00:08 UTC
    I've been caught on this before. You need brackets:-
    @{$arr[0]} = (4,3,2,1);
    You are trying to access (or set in this example) the array within $arr[0]. Without the brackets Perl tries @{$arr}->[0] which is different.
    Remember this in future situations, it could be much longer, such as:-
    %{$scalar_ref->[0]->{hashkey}->[5]}
    Particularly if you are trying to do a foreach loop:-
    foreach my $key (keys %$scalar_ref->[0]->{hashkey}->[5]) {
    Wont work, whereas:-
    foreach my $key (keys %{$scalar_ref->[0]->{hashkey}->[5]}) {
    Will.
    Hope that helps

    Lyle