perl - Parsing comma separated lines and calculating sum -


So my problem can basically be written in pseudo-code:

  Split = Using the first value =, give this value to the next line = check the values ​​after it, if not, then loop up to the end of the file, collect all the values, that match the line numbers and Use, get the last 2 column value sum, for the set given with equal key = value pair I have a dataset as follows:  
  3 = 5002, 0 = 10002, 5 = 1, 4 = 1, 7 = 1, 8 =  

1, 9 = 0, 1 = 14002, 6 = 5, 200, 100 3 = 5002, 0 = 10002, 5 = 0, 4 = 1, 7 = 0, 8 = 0, 9 = 1, 1 = 14002, 6 = 5, 300, 10 = 3, 5, 0 = 10001, 5 = 0, 4 = 0, 7 = 0, 8 = 0, 9 = 0, 1 = 14001, 6 = 3, 1000, 80 3 = 5001, 0 = 10004, 5 = 1, 4 = 1, 7 = 2, 8 = 2, 9 = 1, 1 = 14001, 6 = 3, 10000, 1200 3 = 5003, 0 = 10004, 5 = 2, 4 = 0, 7 = 2, 8 = 2, 9 = 1, 1 = 14003, 6 = 8, 5000, 500 3 = 5003, 0 = 10004, 5 = 3, 4 = 1, 7 = 2, 8 = 1, 9 = 0 , 1 = 14003, 6 = 8, 1000, 7

What do I need to do, say all for 3, which are equal and the sum of the last 2 columns and the value of that value For example:

  3 = 5002, amount = 500, 110 5 = 0, zodiac = 1300, 90 8 = 2, zodiac = 15000, 1700  

I have been able to parse the first 3, but for the remaining columns I I am unable to do anything: - (

Based on my understanding, there are two possible methods. The first one uses the overall key to store values ​​in a single-level hash. The second uses a multi-level hash:

Method 1:

  #! Use / usr / bin / perl strict; use caution; List :: UTL qw (amount); My% data; While (my $ line = & lt; data & gt;) {chomp $ line; My @parts = split /, /, $ line; Last until @pars; My $ value = pop @parts; Push @ {$ data {$ _}}, $ value @ $ $ {{Colle}}}}} $ __ DATA__ 3 = 5002, 0 = 10002, 5 = 1, 4 = 1, 7 = 1, 8 = 1 , 9 = 0, 1 = 14002, 6 = 5, 200 3 = 5002, 0 = 10002, 5 = 0, 4 = 1, 7 = 0, 8 = 0, 9 = 1, 1 = 14002, 6 = 5, 300 3 = 500 1, 0 = 10001, 5 = 0, 4 = 0, 7 = 0, 8 = 0, 9 = 0, 1 = 14001, 6 = 3, 1000 3 = 5001, 0 = 10004, 5 = 1 , 4 = 1, 7 = 2, 8 = 2, 9 = 1, 1 = 14001, 6 = 3, 10000 3 = 5003, 0 = 10004, 5 = 2, 4 = 0, 7 = 2, 8 = 2, 9 = 1, 1 = 14003, 6 = 8, 5000 3 = 5003, 0 = 10004, 5 = 3, 4 = 1, 7 = 2, 8 = 1, 9 = 0, 1 = 14003, 6 = 8, 1000 C: \ Temp> HJ3 = 5001: 110003 = 5002: 5003 = 5003: 6000 0 = 10001: 1000 = 10002: 500 0 = 10004: 16000 1 = 14001: 11000 1 = 14002: 500 1 = 14003: 6000 4 = 0: 6000 4 = 1: 11500 5 = 0: 1300 5 = 1: 10200 5 = 2: 5000 5 = 3: 1000 6 = 3: 11000 6 = 5: 500 6 = 8: 6000 7 = 0: 1300 7 = 1: 200 7 = 2: 16000 8 = 0: 1300 8 = 1: 1200 8 = 2: 15000 9 = 0: 2200 9 = 1: 15300  

Method: 2

  #! Use / usr / bin / perl strict; Use warnings; Use the list: Utility qw (yoga); My Information; While (my $ line = & lt; data & gt;) {chomp $ line; My @ actually = division /, /, $ line; Not until last @ my $ value = $ parts [-1]; (My $ i = 0; $ i & lt; @parts - 2; ++ $ i) {my @subparts = split / = /, $ parts [$ i]; Push @ {$ data {$ subparts [0]} - & gt; {$ Subparts [1]}}, $ value; }} $ $ 1 (Key% data) {$ $ $ {{$ k1}} amount @ {$ data {$ k1} - & gt; {$ K2}}); }} __DATA__3 = 5002, 0 = 10002, 5 = 1, 4 = 1, 7 = 1, 8 = 1, 9 = 0, 1 = 14002, 6 = 5, 200 3 = 5002, 0 = 10002, 5 = 0 , 4 = 1, 7 = 0, 8 = 0, 9 = 1, 1 = 14002, 6 = 5, 300 3 = 5001, 0 = 10001, 5 = 0, 4 = 0, 7 = 0, 8 = 0, 9 = 0, 1 = 14001, 6 = 3, 10003 = 5001, 0 = 10004, 5 = 1, 4 = 1, 7 = 2, 8 = 2, 9 = 1, 1 = 14001, 6 = 3, 10000 3 = 5003, 0 = 10004, 5 = 2, 4 = 0, 7 = 2, 8 = 2, 9 = 1, 1 = 14003, 6 = 8, 5000 3 = 5003, 0 = 10004, 5 = 3, 4 = 1, 7 = 2, 8 = 1, 9 = 0, 1 = 14003, 6 = 8, 1000 C: \ Temp> HJK 3: 5003: 6000 3: 5002: 500 3: 5001: 11000 7: 1: 200 7: 0: 1300 7: 2: 16000 9: 1: 15300 9: 0: 2200 8: 1: 1200 8: 0: 1300 8: 2: 15000 4: 1: 11500 4: 0: 6000 1: 14001: 11000 1: 14003: 6000 1: 14002: 500 0: 10001: 1000 0: 10004: 16000 0: 10002: 500 5: 1: 10200 5: 3: 1000 5: 0: 1300 5: 2: 5000  

NB: sort for taste.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -