Hello, I've been trying to figure out/understand how to create hash of hashes from an input file. I want to be able to create references to each element using variables. Not exactly sure how to actually create the hashes from an input file. Here is a small sample of what my data file looks like my actual file contains all my users data:
FullName : User1 Home Address : 111 address lane Phone : 555-555-5555 FullName : User2 Home Address : 222 address lane 2 Phone : 777-777-7777
Here is the code that I've been playing with so far:
#!/usr/bin/perl use strict; my $myfile = $ARGV[0]; open FILE, "<", $myfile || die "No open $!\n"; my %hash; while (<FILE>) { chomp; my ($key, $val) = split /:/; $hash{$key} .= exists $hash{$key} ? ",$val" : $val; } foreach my $key (keys %hash) { print "$key = $hash{$key}\n"; }
Trying to create groupings name, address, phone using the LABELS on the left side ("FullName", "Home Address", "Phone"). I am trying to use the key $name and print all data for that $name variable. output example:
User: User1 111 address lane 555-555-5555 User: User2 222 address lane 2 777-777-7777
This way I can actually map all FullNames to actual names all Phone to Phone number etc.. I tried creating a reference to an anonymous hash, but the problem is not sure how to only store all names all phone# etc in each hash.
$hash{$x}= { Name => $name, Addr => $address, Phone => $phone
Not sure how to go about this any help would be very much appreciated as I am not getting any where here. I can accomplish this using awk/cut but I started to play around with it in Perl and never really used hashes so I thought this would be a great time to learn/try to understand it. Thanks for all the help in advance. FYI:

In reply to creating hash of hashes from input file by perlnewbie9292

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.