fseng has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, I'm just starting learning Perl. Here is my Q. I want to write a program to read in the following fixed width data file into a data structure of a real array of hashes, the data file for example called "clients.dat" is like following:
Surname L20, First Name L20, Town L20 -------------------------------------------------- Chaplin Charlie Basel Estevez Emilio Santa Manica Sarte Jean Paul Montmarte Rikard Frank Amsterdam Rodin Paul Montmarte
With many help from you guys here I can read in the file but now I have two more question.
Q1:I want to print all the lines sorted by the Town. Like this
Town Surname Firstname Basel Chaplin Charlie Manica Estevez Emilio Santa Montmarte Sarte Jean Paul Montmarte Rodin Paul Amsterdam Rikard Frank
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:
my $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"; }
But this doesnt work somehow. Can anyone help please

Replies are listed 'Best First'.
Re: An REAL array of hashes
by moritz (Cardinal) on Jun 24, 2009 at 06:36 UTC
    I want to print all the lines sorted by the Town. Like this
    Town Surname Firstname Basel Chaplin Charlie Manica Estevez Emilio Santa Montmarte Sarte Jean Paul Montmarte Rodin Paul Amsterdam Rikard Frank

    This doesn't look sorted alphabetically, and in any other way, just with the columns in a different order.

    Maybe Text::Table can help you a bit.

    As for your second question, please use strict; use warnings; - the warnings tell you where it first went wrong.

    my @elems = $key; my $client = $client->[$elems[1]-1];

    This produces the warnings Use of uninitialized value $elems[1] in subtraction (-) at foo.pl line 25.

    That's because you assign one item to the array @elems, and then access the second item (with index 1), which is always undef.

    Somehow I can't make any sense of what you try to do in here.

    if ($a =~ /[Basel Manica Montmarte Amsterdam]/) {

    You've clearly never tested that with an invalid town name. You should. And then read perlre on what the square brackets mean in regular expressions.

    From a more high-level perspective I think your problem is that you write too much code that relies on the variables you filled earlier, without looking if the variables contain what you think they do.

    A great tool for debugging is Data::Dumper, which will produce Perl code that reflects your data structure, for example:

    use Data::Dumper; my @a = (2, 5, 7); print Dumper \@a;

    produces

    $VAR1 = [ 2, 5, 7 ];

    Use that to check if your variables contain what you think they should.

Re: An REAL array of hashes
by Khen1950fx (Canon) on Jun 24, 2009 at 07:12 UTC
    Perl has a bunch of tools that can help you. For starters, always start your code with the shebang line:

    #!/usr/env/perl

    Next, always use strictures---something like this:

    #!/usr/env/perl use strict; use warnings; use diagnostics;

    Next, you can run your script using the perl debugger

    perl -d yourscript.pl

    The debugger will start the script and a prompt will popup. At the prompt, enter the first line of code

    x my $data = "clients.dat";

    Something like 'clients.dat' will come back, so there's the first error: "clients.dat" should be 'clients.dat'. Be sure to use x my $data = "clients.dat" because the "x" will make it correct the mistake for you. If you just enter "my $data = "clients.dat"", then nothing will happen. Pretty simple, no? Enter each line thru the end of the script.

    Next, you'll want to straighten-up your code, make it prettier so to speak. Download and install Perl::Tidy. Then from the command line: perltidy yourscript.pl

    Lastly, since you are new to Perl, you'll want to download and install Perl::Critic. Then enter on the command line:

    perlcritic yourscript.pl

    Be sure to check the documentation. There's a lot that you can do with perlcritic, and it will definitely help you.

      Something like 'clients.dat' will come back, so there's the first error: "clients.dat" should be 'clients.dat'.

      Since both work, I wouldn't call that an error. It's merely a matter of style.