spikeinc has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to loop through the hash function $structure such that the items in the @field is printed once for the unique entries in the __DATA__

My logic below is wrong as this prints me 1 entry of the same information 14 times (there are 14 elements/keys in $structure). I need to loop through $structure that it prints the values of @fields unique number of times. For example there are only 3 instances of values for each key in the __DATA__ .
How can I loop through the structure to print only those 3 entries?

use strict; use warnings; use JSON; use Data::Dumper; my $j = JSON->new; my $structure = $j->decode( do { local $/ = "}"; <DATA> } ); my @fields = qw(eTime lTime eCompressd eUnCompressd iCompressd iUnComp +ressd); while(my $k = each %$structure) { for my $field (@fields){ print qq{$field = $structure->{$field}\n }; } print "\n"; } __DATA__ {"ingCloConn":0,"bySeries":[[1390802201000,1107,4492,908,2708],[139080 +2181000,401,1632,359,1472],[1390802161000,1184,4636,976,3124],[139080 +2141000,1128,4540,952,3028],[1390802121000,253,1040,223,960],[1390802 +101000,1156,4588,978,3076],[1390802081000,1187,4636,995,3124],[139079 +8941000,297,880,277,1072],[1390798921000,1184,4588,958,3076],[1390798 +901000,1298,4988,1075,3476],[1390798881000,75755,11280,16141184,24986 +768],[1390798861000,21483,7372,6353084,6505012],[1390798841000,1055,4 +348,914,3188],[1390798821000,403,1376,352,1216],[1390798801000,1186,4 +684,1021,3172],[1390798781000,1134,4796,932,3012],[1390798761000,372, +1328,330,1168],[1390798741000,1062,4092,914,2932],[1390798721000,1169 +,4892,1017,3124],[1390798701000,412,1376,363,1472],[1390798681000,116 +8,4636,991,3124],[1390798661000,1105,4748,910,2708],[1390798641000,38 +0,1328,325,1424],[1390798621000,1041,4092,895,2932]],"eCompressd":226 +27786,"pConne":0,"iNewConn":0,"eUnCompressd":31929200,"iUnCompressd": +643408,"iCompressd":255554,"lTime":1390802201000,"eCloConn":0,"connec +tionSeries":[[1390802201000,0,0],[1390802181000,0,0],[1390802161000,0 +,0],[1390802141000,0,0],[1390802121000,0,0],[1390802101000,0,0],[1390 +802081000,0,0],[1390802061000,0,0],[1390802041000,0,0],[1390802021000 +,0,0],[1390802001000,0,0],[1390801981000,0,0],[1390801961000,0,0],[13 +90801941000,0,0],[1390801921000,0,0],[1390801901000,0,0],[13908018810 +00,0,0],[1390801861000,0,0],[1390801841000,0,0],[1390801821000,0,0],[ +1390801801000,0,0],[1390801781000,0,0],[1390799041000,0,0],[139079902 +1000,0,0],[1390799001000,0,0],[1390798981000,0,0],[1390798961000,0,0] +,[1390798941000,0,0],[1390798921000,0,0],[1390798901000,0,0],[1390798 +881000,0,0],[1390798861000,0,0],[1390798841000,0,0],[1390798821000,0, +0],[1390798801000,0,0],[1390798781000,0,0],[1390798761000,0,0],[13907 +98741000,0,0],[1390798721000,0,0],[1390798701000,0,0],[1390798681000, +0,0],[1390798661000,0,0],[1390798641000,0,0],[1390798621000,0,0]],"eN +ewConn":0,"eTime":1390798621000,"connLim":600}{"ingCloConn":0,"bySeri +es":[[1390805821000,1003,4252,732,2032],[1390805801000,582,1968,598,2 +516],[1390805781000,1149,4540,929,2756],[1390805761000,912,3756,674,1 +888],[1390805741000,603,2016,632,2564],[1390805721000,1154,4588,1006, +3076],[1390805701000,1000,4252,738,2032],[1390805681000,433,1376,476, +2004],[1390805661000,1191,4636,1003,3124],[1390805641000,984,4252,730 +,2032],[1390805621000,556,1664,580,2468],[1390805601000,1154,4844,955 +,3076],[1390805581000,848,3660,607,1520],[1390805561000,580,1712,606, +2516],[1390805541000,1203,4892,996,3124],[1390805521000,963,4204,699, +1712],[1390805501000,583,1712,604,2516],[1390802501000,1227,4636,984, +3124],[1390802481000,493,1840,350,896],[1390802461000,95343,15644,213 +37087,29392020],[1390802441000,1132,4844,944,3076],[1390802421000,403 +,1376,353,1216],[1390802401000,1189,4636,996,3124],[1390802381000,120 +3,4844,986,3060],[1390802361000,385,1328,320,1168],[1390802341000,104 +9,4092,909,2932],[1390802321000,1177,4892,998,3380],[1390802301000,41 +7,1376,356,1216],[1390802281000,1218,4636,1010,3124],[1390802261000,1 +206,4940,1001,3428],[1390802241000,278,832,259,1024]],"eCompressd":21 +470491,"pConne":0,"iNewConn":0,"eUnCompressd":29827772,"iUnCompressd" +:637636,"iCompressd":253899,"lTime":1390805821000,"eCloConn":0,"conne +ctionSeries":[[1390805821000,0,0],[1390805801000,0,0],[1390805781000, +0,0],[1390805761000,0,0],[1390805741000,0,0],[1390805721000,0,0],[139 +0805701000,0,0],[1390805681000,0,0],[1390805661000,0,0],[139080564100 +0,0,0],[1390805621000,0,0],[1390805601000,0,0],[1390805581000,0,0],[1 +390805561000,0,0],[1390805541000,0,0],[1390805521000,0,0],[1390805501 +000,0,0],[1390805481000,0,0],[1390805461000,0,0],[1390805441000,0,0], +[1390805421000,0,0],[1390805401000,0,0],[1390805381000,0,0],[13908053 +61000,0,0],[1390805341000,0,0],[1390805321000,0,0],[1390805301000,0,0 +],[1390805281000,0,0],[1390805261000,0,0],[1390805241000,0,0],[139080 +2981000,0,0],[1390802961000,0,0],[1390802941000,0,0],[1390802921000,0 +,0],[1390802901000,0,0],[1390802881000,0,0],[1390802861000,0,0],[1390 +802841000,0,0],[1390802821000,0,0],[1390802801000,0,0],[1390802781000 +,0,0],[1390802761000,0,0],[1390802741000,0,0],[1390802721000,0,0],[13 +90802701000,0,0],[1390802681000,0,0],[1390802661000,0,0],[13908026410 +00,0,0],[1390802621000,0,0],[1390802601000,0,0],[1390802581000,0,0],[ +1390802561000,0,0],[1390802541000,0,0],[1390802521000,0,0],[139080250 +1000,0,0],[1390802481000,0,0],[1390802461000,0,0],[1390802441000,0,0] +,[1390802421000,0,0],[1390802401000,0,0],[1390802381000,0,0],[1390802 +361000,0,0],[1390802341000,0,0],[1390802321000,0,0],[1390802301000,0, +0],[1390802281000,0,0],[1390802261000,0,0],[1390802241000,0,0]],"eNew +Conn":0,"eTime":1390802241000,"connLim":600}{"ingCloConn":0,"bySeries +":[[1390809441000,1083,4140,939,3236],[1390809421000,1006,4252,732,20 +32],[1390809401000,556,1920,584,2212],[1390809381000,1125,4492,907,29 +64],[1390809361000,1013,4300,762,2080],[1390809341000,440,1168,500,20 +68],[1390809321000,1204,4940,1025,3428],[1390809301000,975,4252,719,2 +032],[1390809281000,579,1712,601,2260],[1390809261000,1207,4940,1021, +3428],[1390809241000,948,4156,675,1664],[1390809221000,443,1168,508,2 +068],[1390809201000,1172,4844,979,3076],[1390806301000,985,4204,689,1 +984],[1390806281000,573,1664,580,2212],[1390806261000,1130,4444,926,3 +284],[1390806241000,993,4252,706,2032],[1390806221000,487,1520,496,17 +96],[1390806201000,1206,4940,1000,3428],[1390806181000,907,3756,625,1 +888],[1390806161000,571,1712,588,2260],[1390806141000,1240,4940,987,3 +428],[1390806121000,1000,4204,691,1712],[1390806101000,594,1664,599,2 +212],[1390806081000,74563,15484,16083428,27292788],[1390806061000,104 +7,4604,756,2384],[1390806041000,588,1712,601,2260],[1390806021000,114 +9,4588,965,3076],[1390806001000,983,4460,712,1968],[1390805981000,475 +,1216,526,2116],[1390805961000,1177,4588,975,3076],[1390805941000,106 +6,4556,736,2336],[1390805921000,570,1664,568,2212],[1390805901000,114 +2,4540,933,2756],[1390805881000,939,4108,707,2240],[1390805861000,578 +,1712,601,2260]],"eCompressd":16216565,"pConne":0,"iNewConn":0,"eUnCo +mpressd":27731136,"iUnCompressd":639488,"iCompressd":233005,"lTime":1 +390809441000,"eCloConn":0,"connectionSeries":[[1390809441000,0,0],[13 +90809421000,0,0],[1390809401000,0,0],[1390809381000,0,0],[13908093610 +00,0,0],[1390809341000,0,0],[1390809321000,0,0],[1390809301000,0,0],[ +1390809281000,0,0],[1390809261000,0,0],[1390809241000,0,0],[139080922 +1000,0,0],[1390809201000,0,0],[1390809181000,0,0],[1390809161000,0,0] +,[1390809141000,0,0],[1390809121000,0,0],[1390809101000,0,0],[1390809 +081000,0,0],[1390809061000,0,0],[1390809041000,0,0],[1390809021000,0, +0],[1390806121000,0,0],[1390806101000,0,0],[1390806081000,0,0],[13908 +06061000,0,0],[1390806041000,0,0],[1390806021000,0,0],[1390806001000, +0,0],[1390805981000,0,0],[1390805961000,0,0],[1390805941000,0,0],[139 +0805921000,0,0],[1390805901000,0,0],[1390805881000,0,0],[139080586100 +0,0,0]],"eNewConn":0,"eTime":1390805861000,"connLim":600}

Replies are listed 'Best First'.
Re: Looping through Hash
by Athanasius (Archbishop) on Jan 27, 2014 at 09:44 UTC

    Is something like this what you’re looking for?

    #! perl use strict; use warnings; use JSON; my @fields = qw(eTime lTime eCompressd eUnCompressd iCompressd iUnComp +ressd); my $j = JSON->new; { local $/ = '}'; while (my $jsonstr = <DATA>) { my $structure = $j->decode($jsonstr); print "\n========== RECORD ===========\n\n"; printf("%-12s = %s\n", $_, $structure->{$_}) for @fields; } } __DATA__ <as before>

    Output:

    19:36 >perl 854_SoPW.pl ========== RECORD =========== eTime = 1390798621000 lTime = 1390802201000 eCompressd = 22627786 eUnCompressd = 31929200 iCompressd = 255554 iUnCompressd = 643408 ========== RECORD =========== eTime = 1390802241000 lTime = 1390805821000 eCompressd = 21470491 eUnCompressd = 29827772 iCompressd = 253899 iUnCompressd = 637636 ========== RECORD =========== eTime = 1390805861000 lTime = 1390809441000 eCompressd = 16216565 eUnCompressd = 27731136 iCompressd = 233005 iUnCompressd = 639488 19:36 >

    Notes:

    1. The OP code reads only the first record, as it calls <DATA> only once.
    2. For efficiency, put things that aren’t changed by the loop outside of it.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Excellent! This is exactly how I wanted it :)
      I thankyou very much Athanasius! Appreciate your help in making me understand

      I will use this as a base and perform the manipulations I need. Good learning for me!
Re: Looping through Hash Function
by hippo (Archbishop) on Jan 27, 2014 at 09:13 UTC

    Since you do not reference $k inside the while loop, nothing inside the while loop will change for each iteration, hence you will get the same output over and over. If you change the contents of the while loop so that they only refer to $k, you might have more success.

Re: Looping through Hash Function
by Anonymous Monk on Jan 27, 2014 at 09:12 UTC