sort by hash key before printing
sort by hash key before printing
(OP)
Last year feherke kindly helped me resolve a problem with printing a complex data structure. I have found the resultant script enormously helpful since then. An influx of work since the corona lockdown has left me with a need to slightly modify the print routine. I have received a number of data files in which the data is not sorted by time of departure and it would greatly help readability to print out the data in departure time order.
The data structure is:
The print routine is:
The sorting done above is still vital and needs to be done. However, if the data file contains data which is not in order by departure time it prints them out in the same unsorted departure time order. I would like to print them out from the earliest to the last departure.
Can anyone assist in changing the above routine?
The data structure is:
CODE
$VAR1 = { 'DepartureTime' => '05:53:00', 'PrivateCode' => '4483928', 'VehicleJourneyTimingLinkRefOrder' => [ [ '4', 'PT2M33S' ], [ '5', 'PT0M37S' ], [ '6', 'PT1M34S' ], [ '7', 'PT1M16S' ] ] };
The print routine is:
CODE
sub print_as_table { my $data = shift; foreach my $item (@{$data->{VehicleJourneyTimingLinkRefOrder}}) { print $item->[0], ' ', $item->[1], ' ', $data->{DepartureTime}, ' ', $data->{PrivateCode}, "\n"; } }
The sorting done above is still vital and needs to be done. However, if the data file contains data which is not in order by departure time it prints them out in the same unsorted departure time order. I would like to print them out from the earliest to the last departure.
Can anyone assist in changing the above routine?
RE: sort by hash key before printing
( For those how did not followed it, the referred thread is thread219-1792321: Assistance with printing complex data structure. )
But that function receives one $VAR1 as parameter and that contains a single DepartureTime. Sure you want to sort by that ?
Feherke.
feherke.github.io
RE: sort by hash key before printing
Mmmm. I suspect my use of your code got more elaborate than I remembered. Here's a couple of actual bits of data I am printing out (the full file has about 20 such entries):
CODE
I hope that makes it clearer.
my full print subroutine is currently:
CODE
This prints out a table for each trip. Here's the 18:33:
CODE
RE: sort by hash key before printing