perl - Writing Hash of Hash -


i have hash containing value.

name of hash pg_hash

    {       'pg_3' => {                   'refund' => '-53702.00',                   'payment' => '1122787.00'                 },       'pg_1' => {                   'refund' => '-72569.00',                   'payment' => '1112523.00'                 },     } 

other have array contains value related hash.

@array = {     pg_id => $pg_id,     sum_type => $sum_type,     sum_total => $sum_total,     payment_ref => $payment_ref,     payment_gateway => $payment_gateway }; 

now want write csv file headers:

pg_id  type    sumtotal   type1    sumtotal 1      refund  -72569.00  payment  1112523.00 3      refund  -53702.00  payment  1122787.00 

i unable write files in single row. tried iterating on parent hash unable match array.

help

#! /usr/bin/env perl use strict; use warnings; use feature 'say'; use perl6::form;  %pg_hash = (     'pg_3' => {         'refund'  => '-53702.00',         'payment' => '1122787.00'     },     'pg_1' => {         'refund'  => '-72569.00',         'payment' => '1112523.00'     }, ); @array = (     {         pg_id           => 1,         sum_type        => 'imaginary',         sum_total       => '1000000.00',         payment_ref     => 'imag',         payment_gateway => '.us'     },     {         pg_id           => 3,         sum_type        => 'imaginary',         sum_total       => '500000.00',         payment_ref     => 'imag',         payment_gateway => '.us'     }, );  "pg_id  type    sumtotal   type1    sumtotal"; (@array) {   $pg = $pg_hash{'pg_' . $_->{pg_id}};   print form "{<<<<} refund  {>>>>>>>}  payment  {>>>>>>>>}",               $_->{pg_id}, $pg->{refund}, $pg->{payment}; }  ''; "pg_id  type      sumtotal   type1       sumtotal"; (keys %pg_hash) {   ($id) = $_ =~ /pg_(\d+)/;   ($info) = grep { $_->{pg_id} == $id } @array;   print form "{<<<<} payment  {>>>>>>>>}  sum total  {>>>>>>>>}",                $id, $pg_hash{$_}{payment}, $info->{sum_total}; } 

output:

pg_id  type    sumtotal   type1    sumtotal 1      refund  -72569.00  payment  1112523.00 3      refund  -53702.00  payment  1122787.00  pg_id  type      sumtotal   type1       sumtotal 3      payment  1122787.00  sum total   500000.00 1      payment  1112523.00  sum total  1000000.00 

perl6::form produces formatted output (which not csv output). follow link learn more here.

this code shows both looping on array reference hash, , looping on keys of hash reference array.

this code awful.

first, references hash generating keys: 'pg_' . $_->{pg_id}. shouldn't able this. hash shouldn't have keys this. if keyed pg_id directly, (so, key of 1 rather 'pg_1'), loop read:

for (@array) {   $id = $_->{pg_id};   print form "{<<<<} refund  {>>>>>>>}  payment  {>>>>>>>>}",               $id, $pg_hash{$id}{refund}, $pg_hash{$id}{payment}; } 

second, references array searching entirety (for multiple potential results rather single result) every key of hash. @array should hash, or should processing (i.e., using first loop) rather looking entries.

third... hash 'hash' in name? array named @array? trust these aren't real names. anyway, better code can written given more knowledge data coming from. instance if you're populating %pg_hash database query, , receiving @array stream of records, should extract 3 database's pg_3 before add hash, , should loop on array rather keys of hash.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -