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!
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.