Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day lightoverhead,

As has already been pointed out, using symbolic references this way is a bad idea. If you're interested purely for academic reasons, see "perlref: Symbolic references"; understanding the refs stricture of the strict pragma would also be useful.

As an alternative, consider the technique in this script (and see the Notes at the end):

#!/usr/bin/env perl use strict; use warnings; use Inline::Files; my %master_hash; my @filehandles = (\*FILE1, \*FILE2, \*FILE3); for (0 .. $#filehandles) { my $fh = $filehandles[$_]; my $wow_key = "wow$_"; while (<$fh>) { chomp; push @{$master_hash{$wow_key}{(split /\t/)[1]}}, 'sth'; } } use Data::Dump; dd \%master_hash; __FILE1__ x file1line1 x file1line2 __FILE2__ x file2line1 x file2line2 __FILE3__ x file3line1 x file3line2

Output:

{ wow0 => { file1line1 => ["sth"], file1line2 => ["sth"] }, wow1 => { file2line1 => ["sth"], file2line2 => ["sth"] }, wow2 => { file3line1 => ["sth"], file3line2 => ["sth"] }, }

Notes

  • I've kept much the same logic as you show in your OP.
  • The wowN names you wanted are created and the hashes they refer to are easily accessible via a $master_hash{wowN} variable.
  • I've had to guess at input data as you didn't show any. For demo purposes, I've used Inline::Files; for your real code, continue to use an array of filenames and open.

-- Ken


In reply to Re: How to correctly use a symbolic reference to automatically generate hash names? by kcott
in thread How to correctly use a symbolic reference to automatically generate hash names? by lightoverhead

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 lurking in the Monastery: (7)
As of 2024-03-28 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found