Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to read a file and add value for same key

by tony_ucdavis (Initiate)
on Jul 31, 2013 at 04:37 UTC ( #1047169=perlquestion: print w/ replies, xml ) Need Help??
tony_ucdavis has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a file name "test" which has: a 1 b 3 c 5 a 2 I want to write a perl script to open and read this file "test" and add value for same key and write out: a 3 b 3 c 5

Comment on How to read a file and add value for same key
Re: How to read a file and add value for same key
by moritz (Cardinal) on Jul 31, 2013 at 04:43 UTC

    Hello, and welcome to perlmonks.

    I want to write a perl script

    Great. Then I suggest you get started. If you're not very familiar with perl, you might want to start with perlintro.

    If you experience problems along the way, feel free to ask here, and make sure to show the code you have written, along with a description of what your problem is.

Re: How to read a file and add value for same key
by nevdka (Monk) on Jul 31, 2013 at 05:30 UTC

    Hi tony_ucdavis,

    I only recently started with Perl myself. The tutorials section has a lot of really good information on how to start writing scripts and programs. Also, see if you can find a copy of the book 'Learning Perl', I have found it very useful.

    You'll want to use the open function to open the file, a while loop to read the data in, and the print function to write everything out. If you want to store the data first, you can use a hash.

Re: How to read a file and add value for same key
by Anonymous Monk on Jul 31, 2013 at 06:29 UTC

    Hi tony ucdavis,
    In case, you think those who commented before now really do not what to help, please think again. What you labour to do will not only be yours but would give you a kind of chip on your shoulder and a well of satisfaction.
    However, if you are still in doubt the following solve your problem:

    use warnings; use strict; my %h; chomp( my @arr = split /\s+?/, <DATA> ); my $key; for (@arr) { if ( !/[[:digit:]]/ ) { $key = $_; } else { if ( $h{$key} ) { $h{$key} += $_; } else { $h{$key} = $_ } } } print $_, q[ ], $h{$_}, q[ ], for sort keys %h; __DATA__ a 1 b 3 c 5 a 2
    But seriously, you need to know, how all that came together. So, please follow the advise given before this post. You would be glad you did.
    And welcome to the Monastery!!!

Re: How to read a file and add value for same key
by Skeeve (Vicar) on Jul 31, 2013 at 09:49 UTC

    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-11-26 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (162 votes), past polls