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

Thanks for the perl Solution. but . . .

I am getting the error "Can't coerce array into hash at" for my $entry (sort {$a->[0] <=> $b->[0]} values %{$data{$state}{entry}}) I googled it and refered "Can't coerce array into hash at", but couldnt fix it. Anybody please help!

Thanks In advance for the help

--VC

Replies are listed 'Best First'.
Re^3: Sorting and Grouping with two different fields
by Limbic~Region (Chancellor) on Jul 12, 2007 at 19:25 UTC
    vcTheGuru,
    As I stated, the code was untested. It worries me that you are testing code you don't understand and that you can't generate your own code using my description of the algorithm. I believe the solution is:
    for my $entry (sort {$a->[0] <=> $b->[0]} @{$data{$state}{entry}}) { # ... }

    Cheers - L~R

      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

        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