×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Assistance with printing complex data structure

Assistance with printing complex data structure

Assistance with printing complex data structure

(OP)
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 

RE: Assistance with printing complex data structure

Hi

As I understand, those $VAR1 and $VAR2 are separate variables, so I would use a sub like this :

CODE --> Perl

sub print_as_table {
    my $data = shift;

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

&print_as_table($VAR1);
&print_as_table($VAR2); 

Feherke.
feherke.github.io

RE: Assistance with printing complex data structure

(OP)
Hi again feherke. That works nicely. Thank you once again!

RE: Assistance with printing complex data structure

(OP)
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.

RE: Assistance with printing complex data structure

(OP)
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.

RE: Assistance with printing complex data structure

Hi

Quote (tonykent)

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 %{ .. } dereferencing has to be avoided and no need to play with manually set order :

CODE --> Perl

sub print_as_table {
    my $data = shift;

    foreach my $key ($data->{VehicleJourneyTimingLinkRef}->Keys) {
        print $key, ' ', $data->{VehicleJourneyTimingLinkRef}->FETCH($key), ' ', $data->{DepartureTime}, ' ', $data->{PrivateCode}, "\n";
    }
} 
Note that I never used Tie::IxHash, so I may be wrong.

Feherke.
feherke.github.io

RE: Assistance with printing complex data structure

(OP)
That's giving me:

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

RE: Assistance with printing complex data structure

Hi

Interesting. This is my test script :

CODE --> Perl

use strict;
use warnings;
use Tie::IxHash;

my $VAR1 = {
    'DepartureTime' => '05:53:00',
    'PrivateCode' => '4483928',
    'VehicleJourneyTimingLinkRef' => Tie::IxHash->new(
        '7' => 'PT1M16S',
        '6' => 'PT1M34S',
        '4' => 'PT2M33S',
        '5' => 'PT0M37S'
    )
};

my $VAR2 = {
    'PrivateCode' => '4483929',
    'DepartureTime' => '06:10:00',
    'VehicleJourneyTimingLinkRef' => Tie::IxHash->new(
        '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'
    )
};

sub print_as_table {
    my $data = shift;

    foreach my $key ($data->{VehicleJourneyTimingLinkRef}->Keys) {
        print $key, ' ', $data->{VehicleJourneyTimingLinkRef}->FETCH($key), ' ', $data->{DepartureTime}, ' ', $data->{PrivateCode}, "\n";
    }
}

&print_as_table($VAR1);
&print_as_table($VAR2); 
Did I misunderstood something ? Do you get error with my test script too ?

Feherke.
feherke.github.io

RE: Assistance with printing complex data structure

(OP)
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.

RE: Assistance with printing complex data structure

(OP)
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!

RE: Assistance with printing complex data structure

Hi

Great. That Tie::IxHash caught me. When added the items one by one, wasn't able to get them back in order. sad

CODE --> Perl

sub print_as_table {
    my $data = shift;

    foreach my $item (@{$data->{VehicleJourneyTimingLinkRefOrder}}) {
        print $item->[0], ' ', $item->[1], ' ', $data->{DepartureTime}, ' ', $data->{PrivateCode}, "\n";
    }
} 

Feherke.
feherke.github.io

RE: Assistance with printing complex data structure

(OP)
That works fine, so I am now all systems go. Thanks again, I really appreciate the help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close