in reply to sorting on multiple columns using Data::Table?

Please show us how you have tried so far using Data::Table, so that we may try to point your eventual syntax errors.

  • Comment on Re: sorting on multiple columns using Data::Table?

Replies are listed 'Best First'.
Re^2: sorting on multiple columns using Data::Table?
by cypress (Beadle) on Feb 25, 2008 at 19:26 UTC
    use Data::Table; $header[0] = "NAME"; $header[1] = "NUMBER"; $data[0][0] = "Mary"; $data[0][1] = 3; $data[1][0] = "John"; $data[1][1] = 2; $data[2][0] = "John"; $data[2][0] = 4; $data[3][0] = "Mary"; $data[3][1] = 2; $table = new Data::Table ($data, $header, 0); # TRIPLETS, FIRST PARAM IS COLUMN INDEX, SECOND IS NUMERICAL VALUE/ # NON-NUMERICAL, THIRD IS ASCENDING/DESCENDING SORT $table->sort(0, 1, 0, 1, 0, 0); # RECONSTITUTE ARRAY $data[0][0] = $table->elm(0,0); $data[0][1] = $table->elm(0,1); $data[1][0] = $table->elm(1,0); $data[1][1] = $table->elm(1,1); $data[2][0] = $table->elm(2,0); $data[2][1] = $table->elm(2,1); $data[3][0] = $table->elm(3,0); $data[3][1] = $table->elm(3,1); print "$header[0]\t$header[1]\n\n"; print "$data[0][0]\t$data[0][1]\n"; print "$data[1][0]\t$data[1][1]\n"; print "$data[2][0]\t$data[2][1]\n"; print "$data[3][0]\t$data[3][1]\n";
    Running it, I get the error message: Row index out of range 0..-1

    Thanks again, cypress

    (And I did make a slight data typo in my original post.)

      You should start by using strict and warnings. And if you did, you might have spotted this. Note that you are assigning values to elements of the array @data and the constructing the object Data::Table with the scalar $data which does not relate to the array, and it is not the array reference the constructor is expecting. The same applies to @header.