Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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! }



In reply to Array reference problem by hda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-06-14 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    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.