Greetings I need to generate a xml document from a nested perl hash, keeping the order of the hash keys. I am trying out https://metacpan.org/pod/Tie::IxHash for the keeping the keys in order part. However I cannot figure out how to access the hash keys that Tie::IxHash creates, they dont behave like a hash key, hash ref key or an object. In the sample code I create a Tie::IxHash and try to print the key Transmitter Here is the sample code:
use strict;
use warnings;
use diagnostics;
use Scalar::Util;
use Data::Dumper;
use XML::Writer;
use Tie::IxHash;
use DateTime::Format::XSD;
my $time = time;
my $dt = DateTime->now;
my $timestamp = DateTime::Format::XSD->format_datetime($dt);
my $transmissionId = sprintf("%u",int(rand(100000000000000000000))). "E";
#sample data structure as hsah ref
# my $transmissionHeader = {
# TransmissionId => "$transmissionId",
# Timestamp => "$timestamp",
# Transmitter => {
# ETIN => '1232456789',
# SOME => '5555555555',
# }
# };
my $transmissionHeader = Tie::IxHash->new(
TransmissionId => "$transmissionId",
Timestamp => "$timestamp",
Transmitter => Tie::IxHash->new( #each nested hash a new object?
ETIN => '1232456789',
SOME => '5555555555',
),
);
print Dumper $transmissionHeader;
# tried all these no joy
print "$transmissionHeader{Transmitter} \n";
print "$transmissionHeader->{Transmitter} \n";
print "$transmissionHeader->Transmitter() \n";
CodePudding user response:
You can use the Indices()
method to get the index of the key, and then use the Values()
method to get the value of that key:
my $idx = $transmissionHeader->Indices('Transmitter');
my $hash = $transmissionHeader->Values($idx);
print Dumper $hash;