Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Looping thru the complex hash and updating the first element of the value

by chanakya (Friar)
on Jun 05, 2006 at 14:59 UTC ( #553611=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks

I complex Hash for which I'm experiencing a problem.

The Problem:
Loop through the Hash items(sub keys)like "13304_21114.01", and if similar key(s) found append the filename of the
second subkey to the first element of the value, i,e for example:

if I find a similar sub key of "13341_27276.01" the value should be updated as :
%csv_data = ( '13341_27276.01' => 'BMVG_ACE.txt / SCVG_ACE.txt / SC_ACE.txt|CONF +IRMED|13304|21114.01|20060602|20060530|2261957|US2471311058|DELGI GRO +UP|DELGI GROUP|US/ USD|US/ USD|DFG UN Eq|STK_DIV|Stock dividend / + Bonus|28616000.000000|42924000.000000|||1.000000|1.000000|[3] / [2]| +|S 1 : 2|||||40301020|40301020|Life & Health |Life & Health |', '13304_21114.01' => 'BMVG_ACE.txt / SC_ACE.txt|CONFIRMED|13304|211 +14.01|20060602|20060530|2261957|US2471311058|DELGI GROUP|DELGI GROUP| +US/ USD|US/ USD|DFG UN Eq|STK_DIV|Stock dividend / Bonus|28616000 +.000000|42924000.000000|||1.000000|1.000000|[3] / [2]||S 1 : 2||||| +40301020|40301020|Life & Health |Life & Health |', '10291_24687.01' => 'BMVG_ACE.txt|CONFIRMED|10291|24687.01|2006060 +2|20060601|2227584|US4534141043|INDEPENDENCE CMNTY ||US/ USD|/|ICBC + UQ Eq|ACQ|Agreement: cas|82332000.000000||||0.900000|||||||||401020 +10||Thrifts & Mortgage Finance||', );
its because the sub key of 13341_27276.01 is seen first in BMVG_ACE.txt. If the subkey is not found in other files it should also be maintained within the hash
with its original value as given in the above example. The final Hash is the output I should be writing to a file.
Below is the hash which contains the sample data:
%csv_data = ( 'BMVG_ACE.txt' => { '13304_21114.01' => 'BMVG_ACE.txt|CO +NFIRMED|13304|21114.01|20060602|20060530|2261957|US2471311058|DELGI G +ROUP|DELGI GROUP|US/ USD|US/ USD|DFG UN Eq|STK_DIV|Stock dividend + / Bonus|28616000.000000|42924000.000000|||1.000000|1.000000|[3] / [2 +]||S 1 : 2|||||40301020|40301020|Life & Health |Life & Health |',<r +eadmore> '13071_27356.01' => 'BMVG_ACE.txt|EX +PECTED|13071|27356.01|20060607|20060515|2407632|US4433201062|HUB GROU +P A|HUB GROUP A|US/ USD|US/ USD|HUBG UQ Eq|STK_DIV|Stock dividend +/ Bonus|19948000.000000|39896000.000000|||0.900000|0.900000|[2] / [1] +||S 1 : 1|||||20301010|20301010|Air Freight & Logistics|Air Freight + & Logistics|', '13341_27276.01' => 'BMVG_ACE.txt|EX +PECTED|13341|27276.01|20060614|20060601|2985514|US9838571035|X-RITE|X +-RITE|US/ USD|US/ USD|XRIT UQ Eq|ACQ|Tender offer: share (or cas a +nd share)|21241000.000000|28583426.000000|||0.900000|0.950000|||||||| +45203010|45203010|Electronic Equipment Manufacturers|Electronic Equip +ment Manufacturers|', '10291_24687.01' => 'BMVG_ACE.txt|CO +NFIRMED|10291|24687.01|20060602|20060601|2227584|US4534141043|INDEPEN +DENCE CMNTY ||US/ USD|/|ICBC UQ Eq|ACQ|Agreement: cas|82332000.000 +000||||0.900000|||||||||40102010||Thrifts & Mortgage Finance||', }, 'EM_ACE.txt' => { '13331_34127.01' => 'EM_ACE.txt|CONFIR +MED|13331|34127.01|20060615|20060601|B154564||| OF CHINA H|/|CN/ HKD| +3988 HK Eq|IPO|IPO||243510368009.000000||0.080000||0.080000|||||1| +|||40101010||Diversified s|', '12888_15186.01' => 'EM_ACE.txt|CONFIR +MED|12888|15186.01|20060607|20060601|6477169|JO3100411011|JORDAN ELEC +TRIC POWER|JORDAN ELECTRIC POWER|JO/ JOD|JO/ JOD|JOEP JR Eq|STK_DI +V|Stock dividend / Bonus|50000000.000000|60000000.000000|0.750000|0.7 +50000|0.750000|0.750000|[1.2] / [1]||S 0.2 : 1|1|1|||55101010|55101 +010|Electric Utilities|Electric Utilities|', }, 'RMS_ACE.txt' => { '13337_25418.01' => 'RMS_ACE.txt|CONF +IRMED|13337|25418.01|20060602|20060601||US9396531017|WASHINGTON REAL +ESTATE|WASHINGTON REAL ESTATE|US/ USD|US/ USD|WRE UN Eq|PUB_OFF|P +ublic offer to institutions|42139000.000000|44853159.000000|||1.00000 +0|1.000000|||Publpl 2600000 @ USD 34.4 ( + UPDATED )|1|1|||40402010|4 +0402010|Diversified REIT\'s|Diversified REIT\'s|' }, 'DMVG_ACE.txt' => { '13219_10955.03' => 'DMVG_ACE.txt|EX +PECTED|13219|10955.03|20060609|20060524||||ELECTROLUX (DETACHED)|/|SE +/ SEK||SPIN|Spun off NOT traded on the ex date||299418000.000000||0.9 +00000|||||||1||1||25201040||Household Appliances|', '12895_27013.01' => 'DMVG_ACE.txt|EX +PECTED|12895|27013.01|20061218|20060504|2695620|KYG372601099|GARMIN|G +ARMIN|US/ USD|US/ USD|GRMN UQ Eq|STK_DIV|Stock dividend / Bonus|10 +8120000.000000|215874000.000000|0.550000|0.550000|||[2] / [1]||S 1 +: 1|1|1|0|0|25201010|25201010|Consumer Electronics|Consumer Electroni +cs|', '13317_12706.01' => 'DMVG_ACE.txt|EX +PECTED|13317|12706.01|20060615|20060531|0807041|GB0008070418|INVENSYS +|INVENSYS|GB/ GBP|GB/ GBP|ISYS LN Eq|RTS|Right of new underlying s +hare|5687363013.000000|7962308218.000000|1.000000|1.000000|||If 0.15 +> P(t-1) then 1 else [P(t-1)] / [(P(t-1) * 5 + 0.3) / 7]||R 2 : 5 @ +GBP 0.15|1|1|0|0|20106020|20106020|Industrial Machinery|Industrial Ma +chinery|', '13338_18538.01' => 'DMVG_ACE.txt|CO +NFIRMED|13338|18538.01|20060606|20060601|4031976|DE0005003404|ADIDAS- +SALOMON|ADIDAS-SALOMON|DE/ EUR|DE/ EUR|ADS GY Eq|SPLIT|Split|5076 +1755.000000|203047020.000000|1.000000|1.000000|||[4] / [1]||Split 4 : + 1|1|1|1|1|25203010|25203010|Apparel; Accessories & Luxury Goods|Appa +rel; Accessories & Luxury Goods|', }, 'EMVG_ACE.txt' => { '13331_34127.01' => 'EMVG_ACE.txt|CO +NFIRMED|13331|34127.01|20060615|20060601|B154564||| OF CHINA H|/|CN/ +HKD|3988 HK Eq|IPO|IPO||243510368009.000000||0.080000||0.080000||| +||1||||40101010||Diversified s|', '12888_15186.01' => 'EMVG_ACE.txt|CO +NFIRMED|12888|15186.01|20060607|20060601|6477169|JO3100411011|JORDAN +ELECTRIC POWER|JORDAN ELECTRIC POWER|JO/ JOD|JO/ JOD|JOEP JR Eq|ST +K_DIV|Stock dividend / Bonus|50000000.000000|60000000.000000|0.750000 +|0.750000|0.750000|0.750000|[1.2] / [1]||S 0.2 : 1|1|1|||55101010|5 +5101010|Electric Utilities|Electric Utilities|', }, 'APVG_ACE.txt' => { '13331_34127.01' => 'APVG_ACE.txt|CO +NFIRMED|13331|34127.01|20060615|20060601|B154564||| OF CHINA H|/|CN/ +HKD|3988 HK Eq|IPO|IPO||243510368009.000000||0.080000|||||||1||||4 +0101010||Diversified s|', '13330_24527.01' => 'APVG_ACE.txt|EX +PECTED|13330|24527.01|20060616|20060601|6501314|JP3258200009|KIRIN BE +VERAGE CORP||JP/ JPY|/|2595 JT Eq|ACQ|Tender offer: cas only|55002 +370.000000||0.400000|||||||0|0|||30201030||Soft Drinks||', '13330_11642.01' => 'APVG_ACE.txt|EX +PECTED|13330|11642.01|20060616|20060601|6493745|JP3258000003|KIRIN BR +EWERY CO|KIRIN BREWERY CO|JP/ JPY|JP/ JPY|2503 JT Eq|ACQ|Tender of +fer: cas only|984508000.000000|984508000.000000|0.800000|0.800000|||| +||1|1|||30201010|30201010|Brewers|Brewers|' }, 'DM_ACE.txt' => { '13219_10955.03' => 'DM_ACE.txt|EXPECT +ED|13219|10955.03|20060609|20060524||||ELECTROLUX (DETACHED)|/|SE/ SE +K||SPIN|Spun off NOT traded on the ex date||299418000.000000||0.90000 +0|||||||1||1||25201040||Household Appliances|', '12895_27013.01' => 'DM_ACE.txt|EXPECT +ED|12895|27013.01|20061218|20060504|2695620|KYG372601099|GARMIN|GARMI +N|US/ USD|US/ USD|GRMN UQ Eq|STK_DIV|Stock dividend / Bonus|108120 +000.000000|215874000.000000|0.550000|0.550000|||[2] / [1]||S 1 : 1| +1|1|0|0|25201010|25201010|Consumer Electronics|Consumer Electronics|' +, }, 'PMVG_ACE.txt' => { '12280_16814.01' => 'PMVG_ACE.txt|EX +PECTED|12280|16814.01|20060612|20060329|2345022|US31410H1014|FEDERATE +D DEPT STORES|FEDERATED DEPT STORES|US/ USD|US/ USD|FD UN Eq|STK +_DIV|Stock dividend / Bonus|272216000.000000|543734000.000000|||1.000 +000|1.000000|[2] / [1]||S 1 : 1|||||25503010|25503010|Department St +ores|Department Stores|', '12895_27013.01' => 'PMVG_ACE.txt|EX +PECTED|12895|27013.01|20061218|20060504|2695620|KYG372601099|GARMIN|G +ARMIN|US/ USD|US/ USD|GRMN UQ Eq|STK_DIV|Stock dividend / Bonus|10 +8120000.000000|215874000.000000|||0.550000|0.550000|[2] / [1]||S 1 +: 1|||||25201010|25201010|Consumer Electronics|Consumer Electronics|' +, }, 'SCVG_ACE.txt' => { '13304_21114.01' => 'SCVG_ACE.txt|CO +NFIRMED|13304|21114.01|20060602|20060530|2261957|US2471311058|DELGI G +ROUP|DELGI GROUP|US/ USD|US/ USD|DFG UN Eq|STK_DIV|Stock dividend + / Bonus|28616000.000000|42924000.000000|||1.000000|1.000000|[3] / [2 +]||S 1 : 2|||||40301020|40301020|Life & Health |Life & Health |', '13027_27037.01' => 'SCVG_ACE.txt|CO +NFIRMED|13027|27037.01|20060602|20060530|2432214|US4357583057|HOLLY C +ORP|HOLLY CORP|US/ USD|US/ USD|HOC UN Eq|STK_DIV|Stock dividend / + Bonus|29008000.000000|58016000.000000|||0.700000|0.700000|[2] / [1]| +|S 1 : 1|||||10102030|10102030| Refining & Marketing| Refining & Ma +rketing|', }, 'SC_ACE.txt' => { '13304_21114.01' => 'SC_ACE.txt|CONFIR +MED|13304|21114.01|20060602|20060530|2261957|US2471311058|DELGI GROUP +|DELGI GROUP|US/ USD|US/ USD|DFG UN Eq|STK_DIV|Stock dividend / B +onus|28616000.000000|42924000.000000|1.000000|1.000000|||[3] / [2]||S + 1 : 2|||||40301020|40301020|Life & Health |Life & Health |', '13320_31110.01' => 'SC_ACE.txt|CONFIR +MED|13320|31110.01|20060605|20060531|2745837|US0400491082|ARENA RESOU +RCES|ARENA RESOURCES|US/ USD|US/ USD|ARD UA Eq|PRIV_PL|Private pl +acement to strategic|13227000.000000|14511655.000000|0.850000|0.80000 +0|||||Privpl 1150000 @ USD 28.04 ( + UPDATE )|||||10102020|10102020| + & Production| & Production|', '13341_27276.01' => 'SC_ACE.txt|EXPECT +ED|13341|27276.01|20060614|20060601|2985514|US9838571035|X-RITE|X-RIT +E|US/ USD|US/ USD|XRIT UQ Eq|ACQ|Tender offer: share (or cas and s +hare)|21241000.000000|28583426.000000|0.900000|0.950000||||||||||4520 +3010|45203010|Electronic Equipment Manufacturers|Electronic Equipment + Manufacturers|', '13036_23817.01' => 'SC_ACE.txt|EXPECT +ED|13036|23817.01|20060614|20060531|5768585|GRS338163009|HYATT REGENC +Y HOTEL||GR/ EUR|/|HYATT GA Eq|ACQ|Tender offer: cas only|84000000. +000000||0.500000|||||||||||25301020||Hotels; Resorts & Cruise Lines|| +' } );
Here is the sample code I'm trying to wrestle with, to get the output given above:
foreach $file ( sort keys %csv_data ) { #file SCVG_ACE.txt foreach $item ( %{$csv_data{$file} }) { #item 13304_21114.01 if (exists($csv_data{$file}{$item})){ #if the item is found ag +ain add the filename to the #first element of the value $csv_data{$file}{$item} = s/^([^\|]*)\|/$1\/$file\|$csv_d +ata{$file}{$item}/; } else { # maintain the normal data } } }
Please suggest some workarounds for the problem.
Thanks very much in advance

Edited by planetscape - added readmore tags

( keep:0 edit:7 reap:0 )

Replies are listed 'Best First'.
Re: Looping thru the complex hash and updating the first element of the value
by liverpole (Monsignor) on Jun 05, 2006 at 16:00 UTC
    Hi chanakya,

    When you say "problem", I'm not clear on whether you mean there's a bug in what you're trying to do (in which case you such clarify what your bug is), or whether you just want help solving the "problem" that you've presented.

    However, I can make a suggestion which might help you in either case.  You are clearly using the same hash for multiple purposes.  While this is not in and of itself a bad thing, it is potentially confusing to the problem at hand, and might be causing difficulties when iterating through the keys of the hash.  So I would suggest that you use multiple hashes for your data, at least in the beginning, where one has contains keys like "13341_27276.01", and the other contains keys which are filenames.

    Once you've iterated through the %csv_data hash, you can then merge them as a final step, if that best serves your purposes.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://553611]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-12-09 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?