perlnewbie9292 has asked for the wisdom of the Perl Monks concerning the following question:
Here is the code that I've been playing with so far:FullName : User1 Home Address : 111 address lane Phone : 555-555-5555 FullName : User2 Home Address : 222 address lane 2 Phone : 777-777-7777
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:#!/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"; }
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.User: User1 111 address lane 555-555-5555 User: User2 222 address lane 2 777-777-7777
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:$hash{$x}= { Name => $name, Addr => $address, Phone => $phone
|
|---|