I have a file with a list of lines like
at 12345 injected value 1 to 'signal_A'
at 12345 injected value 0 to 'signal_B'
at 12346 injected value 1 to 'signal_A'
at 12348 injected value 1 to 'signal_A'
at 12350 injected value 0 to 'signal_A'
at 12354 injected value 0 to 'signal_A'
From this file, I want to read till the end of the file and I want to build a hash of hashes something like
%tab = (
12345 => {
signal => "signal_A",
value => "1",
},
12345 => {
signal => "signal_B",
value => "1",
},
);
Also I want to iterate this hash table.
Will highly appreciate your help.
You have two elements with the same key. That data structure cannot exist. How about the following instead:
%tab = (
12345 => [
{
signal => "signal_A",
value => "1",
},
{
signal => "signal_B",
value => "1",
},
],
12346 => [
{
signal => "signal_A",
value => "1",
},
],
...
);
You'd use the following to created it
push #{ $tab{$id} }, { signal => $signal, value => $value };
You can iterate over the structure using
for my $id (keys %tab) {
for $event (#{ $tab{$id} }) {
...$event->{signal}...;
...$event->{value}...;
}
}
Related
Here's the structure that I'm trying to access
Dumper $resourceAudit
$VAR1 = '{
\'rh6\' => {
\'h\' => 1,
\'n\' => 1
},
\'win2k8\' => {
\'h\' => 1,
\'n\' => 1
},
\'win2k12\' => {
\'h\' => 3,
\'n\' => 3
},
\'win2k3\' => {
\'h\' => 0,
\'n\' => 1
},
\'usim\' => {
\'h\' => 4,
\'n\' => 4
}
}';
So, I know that $resourceAudit is actually a string and so, %$resourceAudit is sure to give me the Can't use string as a HASH reference error.
Is there any way I can get around this and access the 'rh6' key?
$resourceAudit doesn't contain a reference to a hash; it contains a string. That string is Perl code that would return a reference to a hash when executed. You can use eval EXPR to run Perl code.
my $data = eval($serialized_data)
or die("Error executing audit code: $#");
... %$data ...
I am trying to loop through each object in an array in Perl and I think I am making an obvious error.
my #members_array = [
{
id => 1234,
email => 'first#example.com',
}, {
id => 4321,
email => 'second#example.com',
}
];
use Data::Dumper;
for my $member ( #members_array ) {
print Dumper( $member );
}
Expected output for first iteration
{
id => 1234,
email => 'first#example.com',
}
Actual output for first iteration
[{
'email' => 'first#example.com',
'id' => 1234
}, {
'email' => 'second#example.com',
'id' => 4321
}];
How do I loop through these elements in the array? Thanks!
[ ... ] is used to create an array reference; you need to use ( ... ) to create an array :
my #members_array = (
{
id => 1234,
email => 'first#example.com',
}, {
id => 4321,
email => 'second#example.com',
}
);
And then the rest of your code will work just fine.
I am getting an output to a query in the format below. I need to get all the values for itemthree.
After trying a lot of different things I was able to extract only the first value. I couldn't figure out how to run it in a loop so as to get the values of all itemthree in the items element.
This is a dump of my data
$VAR1 = {
one => { msgSize => 103 },
two => {
items => [
{ itemOne => -1, itemthree => "AB_CD_EF", itemtwo => 0 },
{ itemOne => -1, itemthree => "XY_YZ_AB", itemtwo => 10 },
],
},
someOtherStuff => "abc",
}
Assuming you have a hash:
my #item3s;
for my $item (#{ $hash{two}{items} }){
push #item3s, $item->{itemthree};
}
print "$_\n" for #item3s;
If it's in fact a hash reference, change $hash{two}{items} to $hash->{two}{items}
I am not sure if I am using the correct terminology here but I am trying to loop through an array and create a hash of arrays and values.
Currently my code looks like this:
my $endResult;
my $list = $arrayRef;
my $hash;
foreach my $hash_ref ( #$list ) {
if ( substr($hash_ref->{ID_NUMBER}, 0, 3) eq 'ABC' ) {
$hash->{'ABC'}{$hash_ref->{ID_NUMBER}}->{'VEHICLES'} = $arrayRef1;
push(#$endResult, $hash);
}
... #more ID number if statementss with different id numbers
and I get an output like this:
[
{
ABC => {
ABC1234 => {
VEHICLES => [
{ X => 11, Y => 0, Z => 12 },
{ X => 2001, Y => 100000, Z => 300 },
],
},
ABC56778 => {
VEHICLES => [
{ X => 1324, Y => 0, Z => 234 },
{ X => 666, Y => 7777, Z => 555 },
],
},
...
But what I want is for ABC to point to array of hashes (ABC1234, ABC46778) instead of how it is. Let me know if I need to clarify better but I cant figure out the right syntax to make this happen when building my hash.
To get your long keys into individual array refs, you need to change the assignment of your data structure. I've added a bunch of example data.
# input data
my $vehicles = [
{
'Z' => '12',
'X' => '11',
'Y' => '0',
},
{
'Z' => '300',
'X' => '2001',
'Y' => '100000',
}
];
my $list = [ { ID_NUMBER => 'ABC1234' }, { ID_NUMBER => 'ABC56778' } ];
# output data
my $endResult;
foreach my $hash_ref (#$list) {
my $hash; # needs to be inside of the loop!
if ( substr( $hash_ref->{ID_NUMBER}, 0, 3 ) eq 'ABC' ) {
push #{ $hash->{'ABC'} }, { # this becomes the array
$hash_ref->{ID_NUMBER} => { # and everything below needs
VEHICLES => $vehicles, # to be constructed directly
}
};
push #$endResult, $hash;
}
}
This will yield the following data structure (output with Data::Printer):
\ [
[0] {
ABC [
[0] {
ABC1234 {
VEHICLES [
[0] {
X 11,
Y 0,
Z 12
},
[1] {
X 2001,
Y 100000,
Z 300
}
]
}
}
]
},
[1] {
ABC [
[0] {
ABC56778 {
VEHICLES var[0]{ABC}[0]{ABC1234}{VEHICLES}
}
}
]
}
]
Please note that your choice of variable names makes this very complicated. It's hard to read and will be hell to maintain later. Always pick variable names based on what they represent. Name them something like $vehicle, $ids or $stuff_we_need_to_take_care_off_later, but not $hash1).
1: Unless you work with a hashing algorithm and we're talking about the result. :)
I have two Array of Hashes: The first contains values for a current time interval and the second contains values for a previous time interval.
#AoHcurrent=
( { node => "ABC",
link => "DEF",
time => "10:00",
value => "100",
},
{
node => "FGH",
link => "IJK",
time => "10:00",
value => "200",
},
);
#AoHprevious=
( { node => "ABC",
link => "DEF",
time => "09:45",
value => "10",
},
{ node => "FGH",
link => "IJK",
time => "09:45",
value => "50",
},
);
I want to now use HTML-Template to present this data. Something like :
NODE LINK VALUE
---------------------
ABC DEF 100(10)
FGH IJK 200 (50)
the values in brackets represent the previous value.
my %html_template_parameters =
( AOHCURRENT => \#AoHcurrent,
AOHPREVIOUS => \#AoHprevious, );
my $html_template=qq{Report.tmpl};
my $html_output=qq{Report.html};
htmlReport($html_template,$html_output,\%html_template_parameters);
where htmlReport is a function that generates the report
I require guidance on defining the Report.tmpl file.
Thanks you in advance
see also http://www.perlmonks.org/?node_id=972954
I gave an example there how this can be solved with HTML::Template::Compiled.
Basically you would navigate through the parameter stash like this:
[%= expr=".AOHPREVIOUS[__index__]{'value'}" %]
or with the classic syntax:
<TMPL_VAR expr=".AOHPREVIOUS[__index__]{'value'}" >
You can't do that with 2 separate lists just with HTML::Template. And trying to do it with HTML::Template::Expr would be a nightmare to maintain. Try collapsing them into a single list where the hash data is merged.