http://www.perlmonks.org?node_id=1047223


in reply to How to read a file and add value for same key

Just for the fun of it:

$_=<>; 1 while (s/(\w)\s(\d+)(.*)(\s\1\s)(\d+)/$1." ".($2+$5).$3/e); print

Example call:

$ cat test a 1 b 3 c 5 a 2 $ ./samekey test a 3 b 3 c 5 $ cat anothertest a 1 b 3 c 5 a 2 a 3 b 3 c 5 $ ./samekey anothertest a 6 b 6 c 10

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e