Hi Monks,
I used to do something like the following at a place of work over a year ago and I just forget the syntax. It's driving me kind of crazy. What I used to have the need to often do is have a hash of hashes data structure. Inner hashes would be named according to a string value that was read from a flat file.
What I would also do is call subroutines and pass the inner hash by reference. And I just don't remember the syntax for doing so. If someone out there would be kind enough to divulge the correct syntax for creation of the inner hash and for passing it by reference to a subroutine as well as WHY that syntax is correct, I'd sure be obliged!
I made up the following application. I have this flat file with a few entries. In order are a name,species,gender,age, and hair color.
I have an outer hash and I want the inner hash to contain the value for name. The inner hashes keys would be species, gender, age, etc.
For the sake of learning the syntax, I want to loop through the flat file and create each inner hash with each iteration. Anyway, here is the contents of the text file:
Tony,human,male,47,gray
Mark,human,male,48,brown
Tyler,dog,male,11,brown
Anthi,human,female,39,black
Spud,cat,male,14,white
Erica,human,female,23,blonde
and here is the code I presently have (I know it's silly, but I am passing everything by reference):
#!/usr/bin/perl -w
use strict;
my %myOuterHash = ();
open (INFO,"<info.txt");
while (<INFO>)
{
my ($name,$species,$gender,$age,$hairColor) = split /,/,$_;
%$myOuterHash{ $name } = ();
addValues( \%$myOuterHash{ $name },\$species,\$gender,\$age,\$hair
+Color);
}
sub addValues
{
my ( $hashNameRef,$speRef,$genRef,$ageRef,$hairColRef ) = @_;
$$hashNameRef{ 'species' } = $$speRef;
print "IN SUB:\t$$hashNameRef{ 'species' }\n";
}
One other thing I was wondering. In my application at my former place of employment, I had many records for the same inner hash. So, I did not create that inner hash until I first checked to see if it already was created. Might you also supply the syntax for this conditional?
Thanks!,
Tony (o2)
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.