Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Array reference problem

by hda (Chaplain)
on Jul 07, 2009 at 15:52 UTC ( [id://777917]=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

I am working with a program in which some arrays must be accessed based on their names. I am experiencing a weird problem since some arrays can be accessed and others not.

The example below should give an impression of how the problem goes. I have some arrays defined on some data and named as "@descriptor_array_(some number)". Then I have other arrays, named as "@array_dummy_(some number)" which I construct with data read from files and some number crunching. The thing is that I must access these arrays recursively based on their names, and for doing that I use a reference of the type:

$name = sprintf "%s%s",'descriptor_array',$array_no; print @$name;

Well, the problem is that the predefined arrays cannot be accessed whereas the others can. Both types are defined using "my" within the main loop of the program, and the loop where I call these arrays has the instruction "no strict 'refs'"

Any thoughts???

UPDATE: $array_no is just an example, in my program the refernce is a string that I compose with sprintf.

my @descriptor_array1 = ( 1, 2, 3, 4 ); my @descriptor_array2 = ( 1, 2, 3, 4 ); my @descriptor_array3 = ( 1, 2, 3, 4 ); my @descriptor_array4 = ( 1, 2, 3, 4 ); .......................... my (@array_dummy1,@array_dummy2,@array_dummy3); foreach my $array_no(@insomearray) { no strict 'refs'; $name = sprintf "%s%s",'descriptor_array',$array_no; print @$name; # prints nothing! $name = sprintf "%s%s",'array_dummy',$array_no; print @$name; # prints "array_dummy! }

Replies are listed 'Best First'.
Re: Array reference problem
by davorg (Chancellor) on Jul 07, 2009 at 16:16 UTC
    I am working with a program in which some arrays must be accessed based on their names.

    You really don't want to do that. No, I mean you really don't want to do that. use strict makes symbolic references illegal for a reason.

    There are a tiny number of advanced techniques where you need to use symbolic references. This isn't one of them.

    In this case, what you really want is a hash.

    #!/usr/bin/perl use strict; use warnings; my %descriptor_array = ( 1 => [ 1, 2, 3, 4 ], 2 => [ 1, 2, 3, 4 ], 3 => [ 1, 2, 3, 4 ], 4 => [ 1, 2, 3, 4 ], ); foreach my $array_no (1 .. 4) { print "$array_no: @{$descriptor_array{$array_no}}\n"; }

    If you alter your code to use a model like this then you'll be far happier.

    perldsc will probably be useful to you.


    See the Copyright notice on my home node.

    Perl training courses

Re: Array reference problem
by ikegami (Patriarch) on Jul 07, 2009 at 16:17 UTC
Re: Array reference problem
by JavaFan (Canon) on Jul 07, 2009 at 16:04 UTC
    $name = sprintf "%s%s",'descriptor_array',$array_no; print @$name;
    Since $name contains a string, @$name will access a package variable - not a lexical variable introduced with my.

    Either use our, or use a hash (or array) of arrays, instead of using the names directly.

      Thanks, that worked. However, I can't understand why one of these two 'families' of arrays can be accessed whereas the other not.
        Because only for package variables does the lookup happen at runtime.
Re: Array reference problem
by SuicideJunkie (Vicar) on Jul 07, 2009 at 16:19 UTC
    Why exactly are you not using an array of arrays or a hash of arrays? Strict frowns on the use of such refs for a good reason.

    Hash of Arrays example:
    my $HoA; $HoA->{CountArray} = [1, 2, 3, 4]; my @AlphaArray = ('a', 'b', 'c', 'd'); $HoA->{AlphaArray} = \@AlphaArray; DoStuff($HoA, 'AlphaArray'); sub DoStuff { my $HoA = shift; my $desiredArrayName = shift; die "There is no such thing as '$desiredArrayName'" unless exists ($ +HoA->{$desiredArrayName}); foreach my $value (@{$HoA->{$desiredArrayName}}) { print "$value\n"; } }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://777917]
Approved by ikegami
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-06-18 08:25 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.