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

Hi all...newbie here desperately needing to sort ... and suffering badly...
#!/usr/bin/perl -w use strict; use Lingua::EN::NameCase qw( nc ); #Where the files are located... my $Location = 'z:/bpermits/data2007/test/marchpermits.txt';<br> my $OutFile = 'z:/bpermits/data2007/test/output.txt'; #Open the files...... open(PERMSIN, $Location) or die "unable to open $Location: $!"; open(OUT, ">$OutFile") or die "unable to open $OutFile: $!"; #Read in the file, using a loop........ my @columns = qw( Permit_Number Street_Number Street_Prefix Street_Nam +e Street_Suffix Lot Block Issue_Date Proposed_Use_Code Permission_To_Code Square +_Feet Estimated_Cost Units Subdivision Zoning Contractor_Name Contractor_Add1 Contractor_Add2 Contractor_C +ity Contractor_State Contractor_Zip Contractor_Phone1 Contractor_Phone2 Owner_Name Owner_Add1 Owner_Add2 Owner_City Owner_State Owne +r_Zip Owner_Phone ); while( my $line = <PERMSIN> ) { chomp( $line ); # populate the hash %thisrecord with the column names and the valu +es from this line my @fields = split( /,/, $line ); my %thisrecord = map { $columns[$_] => $fields[$_] } ( 0 .. $#fiel +ds ); my @data = (); push @data, \%thisrecord; }
I just have no idea where to go from here.... I know I need the following line at some point, but don't know where...
sort { $b->{ Estimated_Cost } <=> $a->{ Estimated_Cost } } @data;

Replies are listed 'Best First'.
Re: Help with sort....
by liverpole (Monsignor) on Apr 11, 2007 at 23:31 UTC
    Hi skins96,

    A couple of comments:

    1. You have a stray "<br>" in your code.
    2. You might want to use Data::Dumper, to display intermediate results.
    3. You probably want to put my @data = (); outside of your loop, so it stays in scope afterwards; I'm sure this was causing you some headaches (which Data::Dumper will often help you catch quickly).

    In order to see what what going on, I added some debugging to your program:

    #!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; # use Lingua::EN::NameCase qw( nc ); #Where the files are located... my $Location = 'z:/bpermits/data2007/test/marchpermits.txt'; my $OutFile = 'z:/bpermits/data2007/test/output.txt'; # Debugging -- I don't have a Z: drive :) (and $OutFile isn't used) $Location = "./m.txt"; #Open the files...... open(PERMSIN, $Location) or die "unable to open $Location: $!"; #Read in the file, using a loop........ my @columns = qw( Permit_Number Street_Number Street_Prefix Street_Name Street_Suffi +x Lot Block Issue_Date Proposed_Use_Code Permission_To_Code Square_Feet Estimated_Cost Units Subdivision Zoning Contractor_Name Contractor +_Add1 Contractor_Add2 Contractor_City Contractor_State Contractor_Zip Contractor_Phone1 Contractor_Phone2 Owner_Name Owner_Add1 Owner_Ad +d2 Owner_City Owner_State Owner_Zip Owner_Phone ); # Debugging -- I don't have time to make up all those values :-/ @columns = qw( Permit_Number Street_Number Estimated_Cost ); my @data = (); while( my $line = <PERMSIN> ) { chomp( $line ); # Populate the hash %thisrecord with the column names and # the values from this line. # my @fields = split( /,/, $line ); my %thisrecord = map { $columns[$_] => $fields[$_] } ( 0 .. $#fiel +ds ); push @data, \%thisrecord; } # Debugging -- look at the data before it's sorted print Dumper(\@data), "\n"; print "Unsorted costs: "; map { print $_->{ Estimated_Cost } . ", " } @data; print "\n\n"; # Here's the sort you want! @data = sort { $a->{ Estimated_Cost } <=> $b->{ Estimated_Cost } } @da +ta; # Debugging -- look at the data after it's sorted print Dumper(\@data), "\n"; print "Sorted costs: "; map { print $_->{ Estimated_Cost } . ", " } @data; print "\n\n";

    And used a smaller data file (only 3 columns):

    1,132,1984 2,93,1748 3,92,1372 4,73,2948 5,83,3105 6,13,1299 7,28,1301 8,35,1305 9,47,1091

    This yields the following results, which is what I think you're looking for:

    $VAR1 = [ { 'Street_Number' => '132', 'Estimated_Cost' => '1984', 'Permit_Number' => '1' }, { 'Street_Number' => '93', 'Estimated_Cost' => '1748', 'Permit_Number' => '2' }, { 'Street_Number' => '92', 'Estimated_Cost' => '1372', 'Permit_Number' => '3' }, { 'Street_Number' => '73', 'Estimated_Cost' => '2948', 'Permit_Number' => '4' }, { 'Street_Number' => '83', 'Estimated_Cost' => '3105', 'Permit_Number' => '5' }, { 'Street_Number' => '13', 'Estimated_Cost' => '1299', 'Permit_Number' => '6' }, { 'Street_Number' => '28', 'Estimated_Cost' => '1301', 'Permit_Number' => '7' }, { 'Street_Number' => '35', 'Estimated_Cost' => '1305', 'Permit_Number' => '8' }, { 'Street_Number' => '47', 'Estimated_Cost' => '1091', 'Permit_Number' => '9' } ]; Unsorted costs: 1984, 1748, 1372, 2948, 3105, 1299, 1301, 1305, 1091, $VAR1 = [ { 'Street_Number' => '47', 'Estimated_Cost' => 1091, 'Permit_Number' => '9' }, { 'Street_Number' => '13', 'Estimated_Cost' => 1299, 'Permit_Number' => '6' }, { 'Street_Number' => '28', 'Estimated_Cost' => 1301, 'Permit_Number' => '7' }, { 'Street_Number' => '35', 'Estimated_Cost' => 1305, 'Permit_Number' => '8' }, { 'Street_Number' => '92', 'Estimated_Cost' => 1372, 'Permit_Number' => '3' }, { 'Street_Number' => '93', 'Estimated_Cost' => 1748, 'Permit_Number' => '2' }, { 'Street_Number' => '132', 'Estimated_Cost' => 1984, 'Permit_Number' => '1' }, { 'Street_Number' => '73', 'Estimated_Cost' => 2948, 'Permit_Number' => '4' }, { 'Street_Number' => '83', 'Estimated_Cost' => 3105, 'Permit_Number' => '5' } ]; Sorted costs: 1091, 1299, 1301, 1305, 1372, 1748, 1984, 2948, 3105,

    It's quite verbose, but I think you can see how I was able to quickly debug it.  Just take out my debugging sections, and you should have a viable solution.

    Oh, and by the way, your Perl code is fairly impressive for a self-proclaimed "newbie".  I couldn't wrap my mind around map until I'd been using Perl for quite a while :-)


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Now, now...I can't take the credit for the map...I had a little help using the hashes, and hence the map. I'm still trying to get my brain wrapped around it.
Re: Help with sort....
by GrandFather (Saint) on Apr 11, 2007 at 22:59 UTC

    It is highly recommended that you use a module such as Text::xSV or Text::CSV to read CSV files. They are much tricker than they seem! If the file is very large you might think about using DBI and DBD::CSV to treat the file as a database.

    Note that:

    my %thisrecord = map { $columns[$_] => $fields[$_] } ( 0 .. $#fiel +ds );

    can be done using a slice:

    @thisrecord{@columns} = @fields;

    Where you go after that depends on what you want to achieve. You may want something like:

    for my $items (sort {$b->{Estimated_Value} <=> $a->{Estimated_Value}} +@data) { ... }

    DWIM is Perl's answer to Gödel