G'day Simon,
Welcome to the Monastery.
In case you weren't aware, neither Perl hash key/value pairs nor JSON object property/value pairs are ordered.
Any ordering of such data needs to be handled explicitly by your code.
There seems to be little correlation between your sample data and what you describe.
This makes it extremely difficult to provide you with a concrete solution.
Instead, I've presented an equivalent problem to the one which I believe is causing you problems;
and a solution which I hope will help you with your own issues.
Different field names from different devices is handled by a lookup table: %device_info.
Each device gives two sets of readings.
These are the same for each device but are referenced by different field names and are all jumbled up
(to represent the unordered nature of Perl hashes and JSON objects).
The output handles the different field names and orders the values so that you can see that the readings are indeed identical.
I've kept the code very simple and in line with what you posted.
If you're unfamiliar with the "@{$readings}{@order}" construct,
see "perldata: Slices".
Here's the code:
#!/usr/bin/env perl
use strict;
use warnings;
my %device_info = (
DEVA => [qw{a b c}],
DEVQ => [qw{q r s}],
DEVX => [qw{x y z}],
);
my $json = [
{
read => [
{ qw{r u3 s f1 q s4} },
{ qw{q s6 r u4 s f2} },
],
dev => 'DEVQ',
rep => 'A',
},
{
read => [
{ qw{y u3 z f1 x s4} },
{ qw{x s6 y u4 z f2} },
],
dev => 'DEVX',
rep => 'B',
},
{
read => [
{ qw{b u3 c f1 a s4} },
{ qw{a s6 b u4 c f2} },
],
dev => 'DEVA',
rep => 'C',
},
];
for my $report (@$json) {
print "Report $report->{rep} - ";
my $device = $report->{dev};
print "Device $device\n";
my @order = @{$device_info{$device}};
for my $readings (@{$report->{read}}) {
print "\t@{$readings}{@order}\n";
}
}
Here's the output:
Report A - Device DEVQ
s4 u3 f1
s6 u4 f2
Report B - Device DEVX
s4 u3 f1
s6 u4 f2
Report C - Device DEVA
s4 u3 f1
s6 u4 f2
|