Perl: How to access Array of Hashes passed to a Subroutine - arrays

Have a perl script reading from an xml, parsing the data into hashes in one sub, outputting an array of hashes and then from main calling a second sub to process the array of hashes.
Data::Dumper shows that everything is being passed properly.
Having a terrible time figuring out why I can't now access the hashes.
use strict;
use warnings;
use Data::Dumper;
my (#sortedData, $value1, $value2);
use subs qw(processData outputData);
#sortedData = processData;
outputData($value1, $value2, \#sortedData);
sub processData{
# Example code
# # Does some processing of xml that results in a hash.
# # That series of hashes is pushed onto an array
my ($item, #results);
# foreach $item ( #{ $rss->{items}){
# my %data = (
# 'first' => $item->{'value'},
# 'second' => $item->{'value'},
# 'third' => $item->{'value'}
# );
# push #results, \%data;
# }
# Essentially the hash is :
#results = (
{'data1'=>810,'data2'=>153,'data3'=>215},
{'data1'=>160,'data2'=>220,'data3'=>604},
{'data1'=>940,'data2'=>103,'data3'=>115},
{'data1'=>100,'data2'=>319,'data3',525},
{'data1'=>500,'data2'=>803,'data3'=>650}
);
return #results;
}
sub outputData{
my ($input1, $input2, #localData) = #_;
print Dumper #localData;
print "\#localData: " . #localData . "\n";
foreach my $i (#localData){
# foreach my $j ($i){
# print $i . "\n" . $j . "\n";
# }
print "\$i: " . $i . "\n";
}
}
The output:
$VAR1 = [
{
'data3' => 215,
'data2' => 153,
'data1' => 810
},
{
'data3' => 604,
'data2' => 220,
'data1' => 160
},
{
'data2' => 103,
'data3' => 115,
'data1' => 940
},
{
'data1' => 100,
'data3' => 525,
'data2' => 319
},
{
'data1' => 500,
'data3' => 650,
'data2' => 803
}
];
#localData: 1
$i: ARRAY(0x80071b30)
I don't understand why the value of #localData is 1 or why the reference for an element of #localData is ARRAY instead of HASH.
I am certainly testing my deeper understanding of PERL arrays for the first time.
I don't understand it as much as I thought.

You pass a reference to the array to the sub, then assign this single scalar to #localData. Fix:
sub outputData{
my ($input1, $input2, $localData) = #_;
print Dumper $localData;
print "\#localData: " . #$localData . "\n";
foreach my $i (#$localData){
...
}
}

Related

Convert an array of strings into a array of arrays of strings

My goal is to convert this
my #array=("red", "blue", "green", ["purple", "orange"]);
into this
my #array = ( ["red"], ["blue"], ["green"], ["purple", "orange"]);
Current test code:
my #array = ("red", "blue", "green", ["purple", "orange"] );
foreach $item ( #array ) {
#if this was Python, it would be as simple as:
# if is not instance(item, array):
# # item wasn't a list
# item = [item]
if(ref($item) ne 'ARRAY'){
#It's an array reference...
#you can read it with $item->[1]
#or dereference it uisng #newarray = #{$item}
#print "We've got an array!!\n";
print $item, "\n";
# keep a copy of our string
$temp = $item;
# re-use the variable but convert to an empty list
#item = ();
# add the temp-copy as first list item
#item[0] = $temp;
# print each list item (should be just one item)
print "$_\n" for $item;
}else{
#not an array in any way...
print "ALREADY an array!!\n";
}
}
# EXPECTED my #array=(["red"], ["blue"], ["green"], ["purple", "orange"]);
print #array , "\n";
foreach $item (#array){
if(ref($item) ne 'ARRAY'){
#
#say for $item;
print "didn't convert properly to array\n";
}
}
The comment about python maps pretty directly to perl.
my #array = ("red", "blue", "green", ["purple", "orange"] );
foreach $item ( #array ) {
#if this was Python, it would be as simple as:
# if is not instance(item, array):
# # item wasn't a list
# item = [item]
if (ref $item ne 'ARRAY') {
$item = [ $item ];
}
}
though using map as in Borodin's answer would be more natural.
I'm wondering why you want to do this, but it's
#array = map { ref ? $_ : [ $_ ] } #array
And please don't call arrays #array; that's what the # is for.
Your comment is ridiculous
#if this was Python, it would be as simple as:
# if is not instance(item, array):
# # item wasn't a list
# item = [item]
If you were familiar with Perl then you wouldn't need to ask the question. You must be aware that there is no one-to-one translation from Python to Perl. Python is much less expressive than either Perl or C, but I can't imagine you demanding a simple conversion to C.
Please get over your bigotry.
If you push the values to a new array, you don't need to do more than evaluate whether or not $item is an arrayref:
#! perl
use strict;
use warnings;
use Data::Dumper;
my #array=("red", "blue", "green", ["purple", "orange"]);
my #new_array;
foreach my $item (#array) {
if ( ref($item) eq 'ARRAY' ) {
push #new_array, $item;
}
else {
push #new_array, [$item];
}
}
print Dumper \#new_array;
Output from Dumper:
$VAR1 = [
[
'red'
],
[
'blue'
],
[
'green'
],
[
'purple',
'orange'
]
];
After a long day of learning more Perl than I ever thought/wanted to learn... here's what I think is a workable solution:
#! perl
use strict;
use warnings;
use Data::Dumper;
my %the_dict = (duts =>
{dut_a => {UDF => 'hamnet'},
dut_b => {UDF => [ '1', '2', '3', ]},
dut_c => {UDF => [ 'Z' ], }});
print Dumper \%the_dict;
foreach my $dut (keys %{$the_dict{duts}}) {
# convert the dut's UDF value to an array if it wasn't already
if ( 'ARRAY' ne ref $the_dict{duts}->{$dut}{UDF} ) {
$the_dict{duts}->{$dut}{UDF} = [ $the_dict{duts}->{$dut}{UDF} ];
}
# now append a new value to the array
push(#{$the_dict{duts}{$dut}{UDF}}, 'works');
}
print Dumper \%the_dict;
when run we see these print-outs:
$VAR1 = {
'duts' => {
'dut_a' => {
'UDF' => 'hamnet'
},
'dut_c' => {
'UDF' => [
'Z'
]
},
'dut_b' => {
'UDF' => [
'1',
'2',
'3'
]
}
}
};
$VAR1 = {
'duts' => {
'dut_a' => {
'UDF' => [
'hamnet',
'works'
]
},
'dut_c' => {
'UDF' => [
'Z',
'works'
]
},
'dut_b' => {
'UDF' => [
'1',
'2',
'3',
'works'
]
}
}
};

Store excel data in an array of hashes

My table structure is :
Name Address
Deepti 1325
Cizwan 324
rikita 567
I have to make array like [{Name->deepti,Address->1325},{Name->Cizwan,Address->324},{Name->Rikita,Address->567}]
The table is in excel data, so multiple rows are there.
Thanks in Advance. I have tried something but not able to proceed.
for my $row_num (2..($max_rows))
{
if(exists $workbook->{'cell'}[1][1])
{
#insert values in the hash
$id1 = $workbook->{'cell'}[1][1];
my $val1 = $workbook->{'cell'}[1][$row_num];
#push values in hash
push (#{$hash1{$id1}},$val1);
}
if(exists $workbook->{'cell'}[2][1])
{
#insert values in the hash
$id2 = $workbook->{'cell'}[2][1];
my $val1 = $workbook->{'cell'}[2][$row_num];
#push values in hash
push (#{$hash2{$id2}},$val1);
}
}
print Dumper \%hash1;
print Dumper \%hash2;
So with your structure, you're looking like an array of anonymous hashes is what you want.
This is simpler than you might think:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my #rows;
chomp ( my #header = split ' ', <DATA> );
while ( <DATA> ) {
chomp;
my %row;
#row{#header} = split;
push #rows, \%row;
}
print Dumper \#rows;
__DATA__
Name Address
Deepti 1325
Cizwan 324
rikita 567
This outputs:
$VAR1 = [
{
'Address' => '1325',
'Name' => 'Deepti'
},
{
'Name' => 'Cizwan',
'Address' => '324'
},
{
'Address' => '567',
'Name' => 'rikita'
}
];
But fitting it into your code - you might want to:
push ( #rows, { Name => $id2, Address => $val } );
This works because putting values in {} like that causes it to return an anonymous hash, which you can then insert into the array.

Iterative Hash Set up

I have the following array...
my #array=("100 2", "300 1", "200 3");
From this array I want to iteratively construct a hash.
Current Script:
my %hash;
foreach (#array) {
my #split = (split /\s+/, $_);
%hash = ("$split[0]", "$split[1]");
}
Current Output:
$VAR1 = {
'200' => '3'
};
This is not what I want. My goal is...
Goal Output:
$VAR1 = {
'100' => '2'
'300' => '1'
'200' => '3'
};
What do I need to do?
I am using: Perl 5, Version 18
Assigning to a hash—something you are doing each pass of the loop—replaces its contents. Replace
%hash = ("$split[0]", "$split[1]");
with
$hash{$split[0]} = $split[1];
Alternatively, replace everything with
my %hash = map { split } #array;

perl hash with array

I did same hash like this:
my %tags_hash;
Then I iterate some map and add value into #tags_hash:
if (#tagslist) {
for (my $i = 0; $i <= $#tagslist; $i++) {
my %tag = %{$tagslist[$i]};
$tags_hash{$tag{'refid'}} = $tag{'name'};
}}
But I would like to have has with array, so when key exists then add value to array.
Something like this:
e.g. of iterations
1,
key = 1
value = "good"
{1:['good']}
2,
key = 1
value = "bad"
{1:['good', 'bad']}
3,
key = 2
value = "bad"
{1:['good', 'bad'], 2:['bad']}
And then I want to get array from the key:
print $tags_hash{'1'};
Returns: ['good', 'bad']
An extended example:
#!/usr/bin/perl
use strict;
use warnings;
my $hash = {}; # hash ref
#populate hash
push #{ $hash->{1} }, 'good';
push #{ $hash->{1} }, 'bad';
push #{ $hash->{2} }, 'bad';
my #keys = keys %{ $hash }; # get hash keys
foreach my $key (#keys) { # crawl through hash
print "$key: ";
my #list = #{$hash->{$key}}; # get list associate within this key
foreach my $item (#list) { # iterate through items
print "$item ";
}
print "\n";
}
output:
1: good bad
2: bad
So the value of the hash element to be an array ref. Once you have that, all you need to do is push the value onto the array.
$hash{$key} //= [];
push #{ $hash{$key} }, $val;
Or the following:
push #{ $hash{$key} //= [] }, $val;
Or, thanks to autovivification, just the following:
push #{ $hash{$key} }, $val;
For example,
for (
[ 1, 'good' ],
[ 1, 'bad' ],
[ 2, 'bad' ],
) {
my ($key, $val) = #$_;
push #{ $hash{$key} }, $val;
}

Sorting Hash of Hashes by value

I have the following data structure
my %HoH = {
'foo1' => {
'bam' => 1,
'zip' => 0,
},
'foo2' => {
'bam' => 0,
'zip' => 1,
'boo' => 1
}
};
I would like to sort KEY1 (foo1 or foo2) by the VALUE stored in 'zip' in order from greatest to least.
Here's how I'm doing it.
use strict; use warnings;
use Data::Dumper;
my #sorted;
foreach my $KEY1 (keys %HoH) {
# sort KEY1 by the value 'zip' maps to in descending order
#sorted = sort {$HoH{$KEY1}{'zip'}{$b} <=>
$HoH{$KEY1}{'zip'}{$a}} keys %HoH;
}
print Dumper(\#sorted);
I'm getting an weird warning: Reference found where even-sized list expected at test.pl line 6.
Also print Dumper(\#sorted); is printing
$VAR1 = [
'HASH(0x1b542a8)'
];
When it should be printing
$VAR1 = [
['foo2', 'foo1']
];
Since foo2 has 1 zip and foo1 has 0 zip.
%HoH is declared as a hash, but is defined as a hashreference. Use parentheses (...) instead of braces {...}.
You don't need to loop through the hash to sort it. Sort will take care of that.
if you sort {...} keys %HoH, then the special variables $a and $b represent the keys of %HoH as it performs the sort.
$a and $b are in reverse order because your expected result is in decreasing order. (Update: Oh I just noticed that you had that in the first place.)
The zip value in the nested hash is $HoH{$KEY}{'zip'}, which is what you should sort by.
use strict;
use warnings;
use Data::Dumper;
my %HoH = (
'foo1' => {
'bam' => 1,
'zip' => 0,
},
'foo2' => {
'bam' => 0,
'zip' => 1,
'boo' => 1
}
);
my #sorted = sort {$HoH{$b}{'zip'} <=> $HoH{$a}{'zip'}} keys %HoH;
print Dumper \#sorted;
Note that the result of this code will give you an array:
$VAR1 = [
'foo2',
'foo1'
];
... not a nested array:
$VAR1 = [
['foo2', 'foo1']
];

Resources