in reply to Re^3: Sorting and Grouping with two different fields
in thread Sorting and Grouping with two different fields

Hi Limbic~Region,

Thanks for the reply. I tested this code earlier also, but didnt get answer properly. ie there is a bug in logic where we assigns to the hash. The output we get is

 1 Virginia Norfolk
 2 Virginia Chesapeake
 3 Virginia Virginia Beach
 6 Virginia Falls Church
 8 Washington Seatle
 9 Washington Spokane
 4 Indiana Evansville
 5 Indiana Fort Wayne
 7 Indiana Indianapolis


This is NOT the o/p I need. So I am still trying to get it correct. (didnt gaveup yet)

"It worries me that you are testing code you don't understand"

Its another way how I am learn. I try to change the code and learn all possibilities :)

Thanks for your help & reply

-- VC

  • Comment on Re^4: Sorting and Grouping with two different fields

Replies are listed 'Best First'.
Re^5: Sorting and Grouping with two different fields
by Limbic~Region (Chancellor) on Jul 15, 2007 at 01:24 UTC
    vcTheGuru,
    There isn't a bug in the logic. The bugs were in the code, which was untested. To show that the logic is correct. Here is the code working on your data set with very minor bug fixes:
    #!/usr/bin/perl use strict; use warnings; my %data; while (<DATA>) { chomp; my @col = split /\|/; if (exists $data{$col[1]}) { $data{$col[1]}{min} = $col[0] if $col[0] < $data{$col[1]}{min} +; push @{$data{$col[1]}{entry}}, \@col; } else { $data{$col[1]}{min} = $col[0]; $data{$col[1]}{entry} = [\@col]; } } for my $state (sort {$data{$a}{min} <=> $data{$b}{min}} keys %data) { for my $entry (sort {$a->[0] <=> $b->[0]} @{$data{$state}{entry}}) + { print "@$entry\n"; } } __DATA__ 1|Virginia|Norfolk 2|Virginia|Chesapeake 3|Virginia|Virginia Beach 4|Indiana|Evansville 5|Indiana|Fort Wayne 6|Virginia|Falls Church 7|Indiana|Indianapolis 8|Washington|Seatle 9|Washington|Spokane
    I see that you have a SQL solution not requiring perl.

    Cheers - L~R