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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assistance with printing complex data structure 1

Status
Not open for further replies.

tonykent

IS-IT--Management
Jun 13, 2002
251
GB
Given this data structure, can anyone help me print it out in the format shown below?
Code:
$VAR1 = {
          'DepartureTime' => '05:53:00',
          'PrivateCode' => '4483928',
          'VehicleJourneyTimingLinkRef' => {
                                             '7' => 'PT1M16S',
                                             '6' => 'PT1M34S',
                                             '4' => 'PT2M33S',
                                             '5' => 'PT0M37S'
                                           }
        };
$VAR2 = {
          'PrivateCode' => '4483929',
          'DepartureTime' => '06:10:00',
          'VehicleJourneyTimingLinkRef' => {
                                             '5' => 'PT0M37S',
                                             '21' => 'PT1M34S',
                                             '7' => 'PT1M16S',
                                             '6' => 'PT1M35S',
                                             '19' => 'PT0M43S',
                                             '4' => 'PT2M32S',
                                             '20' => 'PT2M19S',
                                             '16' => 'PT1M43S',
                                             '15' => 'PT0M55S',
                                             '18' => 'PT1M32S',
                                             '22' => 'PT1M36S',
                                             '23' => 'PT1M10S',
                                             '24' => 'PT0M40S',
                                             '17' => 'PT0M48S'
                                           }
        };
The required output format is shown below. The column titles are shown simply for clarity, I don't need these printing.
Code:
VJTL                     Private
Ref      Time	Departure  Code 	
4	PT2M33S	05:53:00 4483928
5	PT0M37S 05:53:00 4483928
6	PT1M34S 05:53:00 4483928
7	PT1M16S 05:53:00 4483928
5	PT0M37  06:10:00 4483929
6	PT1M35S 06:10:00 4483929
7	PT1M16S 06:10:00 4483929
15	PT0M55S 06:10:00 4483929
16	PT1M43S 06:10:00 4483929
etc
 
Hi

As I understand, those $VAR1 and $VAR2 are separate variables, so I would use a sub like this :
Perl:
[b]sub[/b] print_as_table [teal]{[/teal]
    [b]my[/b] [navy]$data[/navy] [teal]=[/teal] [b]shift[/b][teal];[/teal]

    [b]foreach my[/b] [navy]$key[/navy] [teal]([/teal][b]sort[/b] [teal]{[/teal][navy]$a[/navy] [i][green]<=>[/green][/i] [navy]$b[/navy][teal]}[/teal] [b]keys[/b] [teal]%{[/teal][navy]$data[/navy][teal]->{[/teal]VehicleJourneyTimingLinkRef[teal]}}) {[/teal]
        [b]print[/b] [navy]$key[/navy][teal],[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]VehicleJourneyTimingLinkRef[teal]}{[/teal][navy]$key[/navy][teal]},[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]DepartureTime[teal]},[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]PrivateCode[teal]},[/teal] [i][green]"\n"[/green][/i][teal];[/teal]
    [teal]}[/teal]
[teal]}[/teal]

[teal]&[/teal][COLOR=orange]print_as_table[/color][teal]([/teal][navy]$VAR1[/navy][teal]);[/teal]
[teal]&[/teal][COLOR=orange]print_as_table[/color][teal]([/teal][navy]$VAR2[/navy][teal]);[/teal]


Feherke.
feherke.github.io
 
Hi again feherke. That works nicely. Thank you once again!
 
Hi feherke, I have discovered that I need to print out the VehicleJourneyTimingLinkRef's in a specific order. For the 2 records I showed above, they have to be printed out in the order shown in VehicleJourneyTimingLinkRefOrder below:
Code:
$VAR1 = {
          'PrivateCode' => '4483928',
          'DepartureTime' => '05:53:00',
          'VehicleJourneyTimingLinkRef' => {
                                             '5' => 'PT0M37S',
                                             '7' => 'PT1M16S',
                                             '6' => 'PT1M34S',
                                             '4' => 'PT2M33S'
                                           },
          'VehicleJourneyTimingLinkRefOrder' => [
                                                  '4',
                                                  '5',
                                                  '6',
                                                  '7'
                                                ]
        };
$VAR2 = {
          'VehicleJourneyTimingLinkRef' => {
                                             '20' => 'PT2M19S',
                                             '4' => 'PT2M32S',
                                             '15' => 'PT0M55S',
                                             '6' => 'PT1M35S',
                                             '24' => 'PT0M40S',
                                             '17' => 'PT0M48S',
                                             '23' => 'PT1M10S',
                                             '5' => 'PT0M37S',
                                             '16' => 'PT1M43S',
                                             '22' => 'PT1M36S',
                                             '18' => 'PT1M32S',
                                             '21' => 'PT1M34S',
                                             '19' => 'PT0M43S',
                                             '7' => 'PT1M16S'
                                           },
          'VehicleJourneyTimingLinkRefOrder' => [
                                                  '15',
                                                  '16',
                                                  '17',
                                                  '18',
                                                  '19',
                                                  '20',
                                                  '21',
                                                  '22',
                                                  '23',
                                                  '24',
                                                  '4',
                                                  '5',
                                                  '6',
                                                  '7'
                                                ],
          'PrivateCode' => '4483929',
          'DepartureTime' => '06:10:00'
        };
I suspect that the best way to deal with this will be to add the VehicleJourneyTimingLinkRef records as an array (2 dimensional?) rather than a hash.

Currently, I am adding them as a hash as follows:
Code:
foreach my $VehicleJourneys ($dom->findnodes('//VehicleJourneys/VehicleJourney')) {
	$VehicleJourneyTimingLink=$VehicleJourneys->findvalue('./VehicleJourneyTimingLink');
	%hash = $VehicleJourneyTimingLink =~ m/(\d+)\s+(\w*[a-zA-Z]+\w*(?:\s+\w*[a-zA-Z]+\w*)*)\b/g; 
	foreach my $key (keys %hash) {
	     $value = $hash{$key};
		 $VJs[$i]->{VehicleJourneyTimingLinkRef}{$key} = $value;
	}
	$i++;
}
I am then printing out as per your subroutine.
Would you concur that using an array would be the best option? If so, can you help with the syntax? I've tried various options but the best I've managed to come up with added all the values as a single dimensional array (a long list) rather than keeping the pair values together.
 
I should have added that the order of the records in %hash is correct at that point because I used Tie::IxHash to maintain the order. I tried the same for %VehicleJourneyTimingLinkRef but that did not work for some reason.
 
Hi

tonykent said:
I should have added that the order of the records in %hash is correct at that point because I used Tie::IxHash to maintain the order.
That make a world of difference. Then just the [tt]%{[/tt] .. [tt]}[/tt] dereferencing has to be avoided and no need to play with manually set order :
Perl:
[b]sub[/b] print_as_table [teal]{[/teal]
    [b]my[/b] [navy]$data[/navy] [teal]=[/teal] [b]shift[/b][teal];[/teal]

    [b]foreach my[/b] [navy]$key[/navy] [teal]([/teal][navy]$data[/navy][teal]->{[/teal]VehicleJourneyTimingLinkRef[teal]}->[/teal]Keys[teal]) {[/teal]
        [b]print[/b] [navy]$key[/navy][teal],[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]VehicleJourneyTimingLinkRef[teal]}->[/teal][COLOR=orange]FETCH[/color][teal]([/teal][navy]$key[/navy][teal]),[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]DepartureTime[teal]},[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]PrivateCode[teal]},[/teal] [i][green]"\n"[/green][/i][teal];[/teal]
    [teal]}[/teal]
[teal]}[/teal]
Note that I never used [tt]Tie::IxHash[/tt], so I may be wrong.

Feherke.
feherke.github.io
 
That's giving me:

Can't call method "Keys" on unblessed reference at myscript.pl line 118.
 
Hi

Interesting. This is my test script :
Perl:
[b]use[/b] strict[teal];[/teal]
[b]use[/b] warnings[teal];[/teal]
[b]use[/b] Tie[teal]::[/teal]IxHash[teal];[/teal]

[b]my[/b] [navy]$VAR1[/navy] [teal]= {[/teal]
    [i][green]'DepartureTime'[/green][/i] [teal]=>[/teal] [i][green]'05:53:00'[/green][/i][teal],[/teal]
    [i][green]'PrivateCode'[/green][/i] [teal]=>[/teal] [i][green]'4483928'[/green][/i][teal],[/teal]
    [i][green]'VehicleJourneyTimingLinkRef'[/green][/i] [teal]=>[/teal] Tie[teal]::[/teal]IxHash[teal]->[/teal][COLOR=orange]new[/color][teal]([/teal]
        [i][green]'7'[/green][/i] [teal]=>[/teal] [i][green]'PT1M16S'[/green][/i][teal],[/teal]
        [i][green]'6'[/green][/i] [teal]=>[/teal] [i][green]'PT1M34S'[/green][/i][teal],[/teal]
        [i][green]'4'[/green][/i] [teal]=>[/teal] [i][green]'PT2M33S'[/green][/i][teal],[/teal]
        [i][green]'5'[/green][/i] [teal]=>[/teal] [i][green]'PT0M37S'[/green][/i]
    [teal])[/teal]
[teal]};[/teal]

[b]my[/b] [navy]$VAR2[/navy] [teal]= {[/teal]
    [i][green]'PrivateCode'[/green][/i] [teal]=>[/teal] [i][green]'4483929'[/green][/i][teal],[/teal]
    [i][green]'DepartureTime'[/green][/i] [teal]=>[/teal] [i][green]'06:10:00'[/green][/i][teal],[/teal]
    [i][green]'VehicleJourneyTimingLinkRef'[/green][/i] [teal]=>[/teal] Tie[teal]::[/teal]IxHash[teal]->[/teal][COLOR=orange]new[/color][teal]([/teal]
        [i][green]'5'[/green][/i] [teal]=>[/teal] [i][green]'PT0M37S'[/green][/i][teal],[/teal]
        [i][green]'21'[/green][/i] [teal]=>[/teal] [i][green]'PT1M34S'[/green][/i][teal],[/teal]
        [i][green]'7'[/green][/i] [teal]=>[/teal] [i][green]'PT1M16S'[/green][/i][teal],[/teal]
        [i][green]'6'[/green][/i] [teal]=>[/teal] [i][green]'PT1M35S'[/green][/i][teal],[/teal]
        [i][green]'19'[/green][/i] [teal]=>[/teal] [i][green]'PT0M43S'[/green][/i][teal],[/teal]
        [i][green]'4'[/green][/i] [teal]=>[/teal] [i][green]'PT2M32S'[/green][/i][teal],[/teal]
        [i][green]'20'[/green][/i] [teal]=>[/teal] [i][green]'PT2M19S'[/green][/i][teal],[/teal]
        [i][green]'16'[/green][/i] [teal]=>[/teal] [i][green]'PT1M43S'[/green][/i][teal],[/teal]
        [i][green]'15'[/green][/i] [teal]=>[/teal] [i][green]'PT0M55S'[/green][/i][teal],[/teal]
        [i][green]'18'[/green][/i] [teal]=>[/teal] [i][green]'PT1M32S'[/green][/i][teal],[/teal]
        [i][green]'22'[/green][/i] [teal]=>[/teal] [i][green]'PT1M36S'[/green][/i][teal],[/teal]
        [i][green]'23'[/green][/i] [teal]=>[/teal] [i][green]'PT1M10S'[/green][/i][teal],[/teal]
        [i][green]'24'[/green][/i] [teal]=>[/teal] [i][green]'PT0M40S'[/green][/i][teal],[/teal]
        [i][green]'17'[/green][/i] [teal]=>[/teal] [i][green]'PT0M48S'[/green][/i]
    [teal])[/teal]
[teal]};[/teal]

[b]sub[/b] print_as_table [teal]{[/teal]
    [b]my[/b] [navy]$data[/navy] [teal]=[/teal] [b]shift[/b][teal];[/teal]

    [b]foreach my[/b] [navy]$key[/navy] [teal]([/teal][navy]$data[/navy][teal]->{[/teal]VehicleJourneyTimingLinkRef[teal]}->[/teal]Keys[teal]) {[/teal]
        [b]print[/b] [navy]$key[/navy][teal],[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]VehicleJourneyTimingLinkRef[teal]}->[/teal][COLOR=orange]FETCH[/color][teal]([/teal][navy]$key[/navy][teal]),[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]DepartureTime[teal]},[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]PrivateCode[teal]},[/teal] [i][green]"\n"[/green][/i][teal];[/teal]
    [teal]}[/teal]
[teal]}[/teal]

[teal]&[/teal][COLOR=orange]print_as_table[/color][teal]([/teal][navy]$VAR1[/navy][teal]);[/teal]
[teal]&[/teal][COLOR=orange]print_as_table[/color][teal]([/teal][navy]$VAR2[/navy][teal]);[/teal]
Did I misunderstood something ? Do you get error with my test script too ?


Feherke.
feherke.github.io
 
Hi feherke, your script runs fine. My code doesn't [mad] :

Code:
tie %hash, 'Tie::IxHash';
tie %VehicleJourneyTimingLinkRef, 'Tie::IxHash';
foreach my $VehicleJourneys ($dom->findnodes('//VehicleJourneys/VehicleJourney')) {
	#create hash
	$PrivateCode=$VehicleJourneys->findvalue('./PrivateCode');
	$VJs[$i]->{'PrivateCode'} = $PrivateCode;
	$DepartureTime=$VehicleJourneys->findvalue('./DepartureTime');
	$VJs[$i]->{'DepartureTime'} = $DepartureTime;
	$VehicleJourneyTimingLink=$VehicleJourneys->findvalue('./VehicleJourneyTimingLink');
	%hash = $VehicleJourneyTimingLink =~ m/(\d+)\s+(\w*[a-zA-Z]+\w*(?:\s+\w*[a-zA-Z]+\w*)*)\b/g; 
	foreach my $key (keys %hash) {
	     $value = $hash{$key};
		 $VJs[$i]->{VehicleJourneyTimingLinkRef}{$key} = $value;
	}
	$i++;
}
Line 2, suppposedly tieing %VehicleJourneyTimingLinkRef, has never worked in my script. Order is not maintained, which is what led me to start thinking about using an array instead.
 
Good morning feherke, to overcome the ordering/bless issues I have changed the construction of my data structure to use an array, so I now have this:

Code:
$VAR1 = {
          'DepartureTime' => '05:53:00',
          'PrivateCode' => '4483928',
          'VehicleJourneyTimingLinkRefOrder' => [
                                                  [
                                                    '4',
                                                    'PT2M33S'
                                                  ],
                                                  [
                                                    '5',
                                                    'PT0M37S'
                                                  ],
                                                  [
                                                    '6',
                                                    'PT1M34S'
                                                  ],
                                                  [
                                                    '7',
                                                    'PT1M16S'
                                                  ]
                                                ]
        };
Could you help with amending the print subroutine? Hopefully this will be the final step!
 
Hi

Great. That [tt]Tie::IxHash[/tt] caught me. When added the items one by one, wasn't able to get them back in order. :-(
Perl:
[b]sub[/b] print_as_table [teal]{[/teal]
    [b]my[/b] [navy]$data[/navy] [teal]=[/teal] [b]shift[/b][teal];[/teal]

    [b]foreach my[/b] [navy]$item[/navy] [teal]([/teal]@[teal]{[/teal][navy]$data[/navy][teal]->{[/teal]VehicleJourneyTimingLinkRefOrder[teal]}}) {[/teal]
        [b]print[/b] [navy]$item[/navy][teal]->[[/teal][purple]0[/purple][teal]],[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$item[/navy][teal]->[[/teal][purple]1[/purple][teal]],[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]DepartureTime[teal]},[/teal] [i][green]' '[/green][/i][teal],[/teal] [navy]$data[/navy][teal]->{[/teal]PrivateCode[teal]},[/teal] [i][green]"\n"[/green][/i][teal];[/teal]
    [teal]}[/teal]
[teal]}[/teal]


Feherke.
feherke.github.io
 
That works fine, so I am now all systems go. Thanks again, I really appreciate the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top