Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort by hash key before printing

Status
Not open for further replies.

tonykent

IS-IT--Management
Jun 13, 2002
251
0
0
GB
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:

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?

 
Hi

( For those how did not followed it, the referred thread is thread219-1792321. )
tonykent said:
departure time order
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
 
Hi again Feherke,

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:
$VAR1 = [
          {
            'JourneyCode' => 'JC1402',
            'JourneyPatternTimingLinkOrder' => [
                                                 'JPL_75-18-_-y08-5-1-O-1-2%PT1M32S%14:03:32%-%Bus Station to Leisure Centre%Oakridge Park%',
                                                 'JPL_75-18-_-y08-5-1-O-1-3%PT1M30S%14:05:02%-%Leisure Centre to Queensway%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-4%PT1M57S%14:06:59%-%Queensway to Knowles Schools%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-5%PT1M1S%14:08:00%-%Knowles Schools to Aylesbury Street%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-6%PT1M43S%14:09:43%-%Aylesbury Street to Staplehall Road%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-7%PT2M6S%14:11:49%-%Staplehall Road to Simpson Road%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-8%PT1M28S%14:13:17%-%Simpson Road to The Mount%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-9%PT43S%14:14:00%-%The Mount to St Thomas Church%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-10%PT2M20S%14:16:20%-%St Thomas Church to Foxton%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-11%PT2M40S%14:19:00%-%Foxton to Milton Keynes Hospital%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-12%PT3M0S%14:22:00%-%Milton Keynes Hospital to Lucas Place%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-13%PT56S%14:22:56%-%Lucas Place to Baskerfield Grove%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-14%PT2M15S%14:25:11%-%Baskerfield Grove to Butterfield Close%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-15%PT32S%14:25:43%-%Butterfield Close to Marshalls Lane%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-16%PT1M17S%14:27:00%-%Marshalls Lane to The Barge Inn%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-17%PT33S%14:27:33%-%The Barge Inn to Pattison Lane%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-18%PT1M49S%14:29:22%-%Pattison Lane to Glebe Roundabout west%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-19%PT57S%14:30:19%-%Glebe Roundabout west to Enmore Roundabout west%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-20%PT1M41S%14:32:00%-%Enmore Roundabout west to Theatre District%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-21%PT3M0S%14:35:00%-%Theatre District to The Point%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-22%PT1M50S%14:36:50%-%The Point to Central Business Exchange%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-23%PT1M52S%14:38:42%-%Central Business Exchange to Santander House%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-24%PT2M1S%14:40:43%-%Santander House to Central Railway Station%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-25%PT4M17S%14:45:00%-%Central Railway Station to The Countryman%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-26%PT50S%14:45:50%-%The Countryman to Burnham Drive Hail & Ride%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-27%PT1M28S%14:47:18%-%Burnham Drive Hail & Ride to Rooksley Roundabout East%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-28%PT1M42S%14:49:00%-%Rooksley Roundabout East to Heelands Local Centre%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-29%PT44S%14:49:44%-%Heelands Local Centre to Langcliffe Drive Hail & Ride%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-30%PT1M2S%14:50:46%-%Langcliffe Drive Hail & Ride to Stantonbury Roundabout South%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-31%PT15S%14:51:01%-%Stantonbury Roundabout South to Stantonbury Roundabout West%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-32%PT59S%14:52:00%-%Stantonbury Roundabout West to Mercers Drive Hail & Ride%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-33%PT1M45S%14:53:45%-%Mercers Drive Hail & Ride to Kingsfold Hail & Ride%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-34%PT52S%14:54:37%-%Kingsfold Hail & Ride to Barry Avenue Hail & Ride%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-35%PT1M23S%14:56:00%-%Barry Avenue Hail & Ride to Marlborough Roundabout%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-36%PT2M0S%14:58:00%-%Marlborough Roundabout to Oakridge Park Local Centre%%',
                                                 'JPL_75-18-_-y08-5-1-O-1-37%PT2M0S%15:00:00%-%Oakridge Park Local Centre to Selkirk Drive%%'
                                               ],
            'JourneyPatternRef' => 'JP_75-18-_-y08-5-1-O-1',
            'DepartureTime' => '14:02:00',
            'DaysOfWeek' => 'MondayToSaturday ',
            'JourneyPatternSectionRef' => 'JPS_75-18-_-y08-5-1-1-O',
            'DaysOfNonOperation' => 'ChristmasDay BoxingDay GoodFriday NewYearsDay MayDay EasterMonday SpringBank '
          },
          {
            'DepartureTime' => '06:59:00',
            'DaysOfWeek' => 'MondayToSaturday ',
            'JourneyPatternTimingLinkOrder' => [
                                                 'JPL_75-18-_-y08-5-2-I-2-2%PT1M14S%07:00:14%-%Oakridge Park Local Centre to Selkirk Drive%CMK Food Centre%',
                                                 'JPL_75-18-_-y08-5-2-I-2-3%PT46S%07:01:00%-%Selkirk Drive to Vendean%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-4%PT3M0S%07:04:00%-%Vendean to Marlborough Roundabout%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-5%PT57S%07:04:57%-%Marlborough Roundabout to Barry Avenue Hail & Ride%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-6%PT25S%07:05:22%-%Barry Avenue Hail & Ride to Kingsfold%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-7%PT24S%07:05:46%-%Kingsfold to Kingsfold Hail & Ride%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-8%PT54S%07:06:40%-%Kingsfold Hail & Ride to Cleveland%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-9%PT20S%07:07:00%-%Cleveland to Mercers Drive Hail & Ride%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-10%PT2M40S%07:09:40%-%Mercers Drive Hail & Ride to Stantonbury Roundabout South%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-11%PT1M8S%07:10:48%-%Stantonbury Roundabout South to The Suffolk Punch%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-12%PT1M19S%07:12:07%-%The Suffolk Punch to Langcliffe Drive%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-13%PT59S%07:13:06%-%Langcliffe Drive to Hasgill Court%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-14%PT54S%07:14:00%-%Hasgill Court to Heelands Local Centre%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-15%PT1M8S%07:15:08%-%Heelands Local Centre to Rooksley Roundabout North%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-16%PT22S%07:15:30%-%Rooksley Roundabout North to Rooksley Roundabout East%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-17%PT1M17S%07:16:47%-%Rooksley Roundabout East to Chesham Avenue Hail & Ride%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-18%PT32S%07:17:19%-%Chesham Avenue Hail & Ride to Burnham Drive%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-19%PT41S%07:18:00%-%Burnham Drive to The Countryman%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-20%PT1M20S%07:19:20%-%The Countryman to Rooksley Retail Park%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-21%PT2M40S%07:22:00%-%Rooksley Retail Park to Central Railway Station%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-22%PT1M38S%07:23:38%-%Central Railway Station to Santander House%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-23%PT1M37S%07:25:15%-%Santander House to Central Business Exchange%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-24%PT1M45S%07:27:00%-%Central Business Exchange to The Point%%',
                                                 'JPL_75-18-_-y08-5-2-I-2-25%PT3M0S%07:30:00%-%The Point to Theatre District%%'
                                               ],
            'JourneyCode' => 'JC0659',
            'JourneyPatternRef' => 'JP_75-18-_-y08-5-2-I-2',
            'JourneyPatternSectionRef' => 'JPS_75-18-_-y08-5-2-2-I',
            'DaysOfNonOperation' => 'ChristmasDay BoxingDay GoodFriday NewYearsDay MayDay EasterMonday SpringBank '
          },
...............

I hope that makes it clearer.

my full print subroutine is currently:

Code:
sub print_general_as_table {
    my $data = shift;
	my ($linkref,$duration,$deptime,$linkname,$anyWaitTime,$Fromdyndest,$Todyndest);
	$undefwarning = "**not defined**";
	printf REPORT "%8s",$data->{DepartureTime};print REPORT "  ";
	printf REPORT "%5s",$data->{JourneyCode};print REPORT " \n";
	printf REPORT "%5s", "Runs: ";
	printf REPORT "%5s",$data->{DaysOfWeek};print REPORT "\n";
		if (defined($data->{SpecialDaysofOperation}) && ($data->{SpecialDaysofOperation} ne "")) {
		printf REPORT "%5s", "Also operates: ";
		printf REPORT "%5s",$data->{SpecialDaysofOperation};print REPORT "\n";
	}
	if (defined($data->{DaysOfNonOperation}) && ($data->{DaysOfNonOperation} ne "")) {
		printf REPORT "%5s", "Not run: ";
		printf REPORT "%15s",$data->{DaysOfNonOperation};print REPORT "\n";
	}
	printf REPORT "%15s","Link Ref" ;print REPORT "                             ";
	printf REPORT "%12s","Link From/To" ;print REPORT "                     ";
	printf REPORT "%30s","FromDestination" ;print REPORT " ";
	printf REPORT "%28s","ToDestination" ;print REPORT "      ";
	printf REPORT "%12s","Duration" ;print REPORT "  ";
	printf REPORT "%12s","Dep.Time" ;print REPORT "     ";
	printf REPORT "%12s","Stand Time" ;print REPORT "\n";
    foreach my $item (@{$data->{JourneyPatternTimingLinkOrder}}) {
		($linkref,$duration,$deptime,$anyWaitTime,$linkname,$Fromdyndest,$Todyndest)=split('%',$item);
		$linkname = dotize(51,$linkname);
		if ($Fromdyndest eq '') {
			$Fromdyndest = $undefwarning;
		}
		if ($Todyndest eq '') {
			$Todyndest = $undefwarning;
		}
     	printf REPORT "%28s" ,$linkref; print REPORT "  ";
		printf REPORT "%54s" ,$linkname; print REPORT "  ";
		printf REPORT "%24s" ,$Fromdyndest; print REPORT "  ";
		printf REPORT "%24s" ,$Todyndest; print REPORT "         ";
		printf REPORT "%8s" ,$duration; print REPORT "        ";
		printf REPORT "%8s" ,$deptime; print REPORT "    ";
		printf REPORT "%8s" ,$anyWaitTime; print REPORT "  ";
		print REPORT "\n";	
    }
	print REPORT "\n";
}

This prints out a table for each trip. Here's the 18:33:

Code:
18:33:00  JC1833 
Runs: MondayToSaturday 
Not run: ChristmasDay BoxingDay GoodFriday NewYearsDay MayDay EasterMonday SpringBank 
       Link Ref                             Link From/To                                    FromDestination                ToDestination          Duration      Dep.Time       Stand Time
   JPL_75-18-_-y08-5-3-O-3-2                           Theatre District to The Point             Oakridge Park           **not defined**           PT3M0S        18:36:00           -  
   JPL_75-18-_-y08-5-3-O-3-3                  The Point to Central Business Exchange           **not defined**           **not defined**          PT1M50S        18:37:50           -  
   JPL_75-18-_-y08-5-3-O-3-4            Central Business Exchange to Santander House           **not defined**           **not defined**          PT1M52S        18:39:42           -  
   JPL_75-18-_-y08-5-3-O-3-5              Santander House to Central Railway Station           **not defined**           **not defined**           PT2M1S        18:41:43           -  
   JPL_75-18-_-y08-5-3-O-3-6               Central Railway Station to The Countryman           **not defined**           **not defined**          PT4M17S        18:46:00           -  
   JPL_75-18-_-y08-5-3-O-3-7             The Countryman to Burnham Drive Hail & Ride           **not defined**           **not defined**            PT50S        18:46:50           -  
   JPL_75-18-_-y08-5-3-O-3-8  ...rnham Drive Hail & Ride to Rooksley Roundabout East           **not defined**           **not defined**          PT1M28S        18:48:18           -  
   JPL_75-18-_-y08-5-3-O-3-9       Rooksley Roundabout East to Heelands Local Centre           **not defined**           **not defined**          PT1M42S        18:50:00           -  
  JPL_75-18-_-y08-5-3-O-3-10  ...elands Local Centre to Langcliffe Drive Hail & Ride           **not defined**           **not defined**            PT44S        18:50:44           -  
  JPL_75-18-_-y08-5-3-O-3-11  ...e Drive Hail & Ride to Stantonbury Roundabout South           **not defined**           **not defined**           PT1M2S        18:51:46           -  
  JPL_75-18-_-y08-5-3-O-3-12  ...ury Roundabout South to Stantonbury Roundabout West           **not defined**           **not defined**            PT15S        18:52:01           -  
  JPL_75-18-_-y08-5-3-O-3-13  ...onbury Roundabout West to Mercers Drive Hail & Ride           **not defined**           **not defined**            PT59S        18:53:00           -  
  JPL_75-18-_-y08-5-3-O-3-14      Mercers Drive Hail & Ride to Kingsfold Hail & Ride           **not defined**           **not defined**          PT1M45S        18:54:45           -  
  JPL_75-18-_-y08-5-3-O-3-15       Kingsfold Hail & Ride to Barry Avenue Hail & Ride           **not defined**           **not defined**            PT52S        18:55:37           -  
  JPL_75-18-_-y08-5-3-O-3-16      Barry Avenue Hail & Ride to Marlborough Roundabout           **not defined**           **not defined**          PT1M23S        18:57:00           -  
  JPL_75-18-_-y08-5-3-O-3-17  ...arlborough Roundabout to Oakridge Park Local Centre           **not defined**           **not defined**           PT2M0S        18:59:00           -  
  JPL_75-18-_-y08-5-3-O-3-18             Oakridge Park Local Centre to Selkirk Drive           **not defined**           **not defined**           PT2M0S        19:01:00           -  

etc
 
Up until recently the data files always contained departures in time order, starting with the earliest in the day, so my manipulation always resulted in data which happened to be in time order. Just recently a few of them have contained data in which the departures are listed in any old order e.g 06:00, 16:45, 17:00, 07:00, 08:00, 09:00, 10:00, 11:00 etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top