fseng has asked for the wisdom of the Perl Monks concerning the following question:
With many help from you guys here I can read in the file but now I have two more question.Surname L20, First Name L20, Town L20 -------------------------------------------------- Chaplin Charlie Basel Estevez Emilio Santa Manica Sarte Jean Paul Montmarte Rikard Frank Amsterdam Rodin Paul Montmarte
Q2: user will input a town name eg.Basel then it should print out the surname of eceryone who lives there. I did something like this:Town Surname Firstname Basel Chaplin Charlie Manica Estevez Emilio Santa Montmarte Sarte Jean Paul Montmarte Rodin Paul Amsterdam Rikard Frank
But this doesnt work somehow. Can anyone help pleasemy $data = "clients.dat"; open (DAT, $data) or die "\"$data\" not existed or can't be opened!\n" +; my @client; # make this an array, not a hash; my @fields = qw/surname firstname town/; while (<DAT>) { next if ( /^Surname/ or /^-/ ); my @values = unpack 'A20A20A20', $_; my %entry = map { $fields[$_] => $values[$_] } ( 0..$#fields ); push @client, \%entry; } my @towns; my $client; foreach my $client (@client){ push @towns, "$client->{'town'}"; } my @sorted_towns = sort @towns; foreach my $key (@sorted_towns) { my @elems = $key; my $client = $client->[$elems[1]-1]; print "$client->{'town'} $client->{'surname'} $client->{'firstname +'}"; } #Print out the surname of people live in the town print "Please enter a town name: "; my $a = <STDIN>; my $town; if ($a =~ /[Basel Manica Montmarte Amsterdam]/) { my $client->{'town'} = $a; print ${$client}[$town] } else{ die "Invalid town name supplied: $a"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: An REAL array of hashes
by moritz (Cardinal) on Jun 24, 2009 at 06:36 UTC | |
|
Re: An REAL array of hashes
by Khen1950fx (Canon) on Jun 24, 2009 at 07:12 UTC | |
by moritz (Cardinal) on Jun 24, 2009 at 07:22 UTC |