CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Summing column value

 



lokesha
New User

Feb 23, 2010, 11:50 PM

Post #1 of 4 (492 views)
Summing column value Can't Post

I'm new to perl programming. I've a csv file as below.

20100221, abc_1, 200
20100221, abc_4, 350
20100221, opq_3, 200
20100221, abc_5, 220
20100221, xyz_1, 500
20100221, abc_2, 500
20100221, abc_3, 100
20100221, xyz_2, 700
20100221, opq_2, 350
20100221, xyz_3, 100
20100221, opq_1, 230

I'm trying to get output as below, by adding matching values:

Output:

20100221, abc, 1370
20100221, xyz, 1300
20100221, opq, 780

I've started coding as below, but not able to continue due to lack of experience.


Code
 


use strict;
use warnings;

my $inputfilename = $ARGV[0]; ### CSV file name. ###

open( my $in_fh, "<", $inputfilename ) or die "Can't open $inputfilename : $!";

while (my $line = <$in_fh>) {
chomp($line);
my @columns = split(',', $line, 2);




Could any of you help me to accomplish my requirement?

Thanks and Regards / Lokesha


rovf
Veteran

Feb 24, 2010, 12:58 AM

Post #2 of 4 (491 views)
Re: [lokesha] Summing column value [In reply to] Can't Post

Your split causes problems.

(1) The first parameter is supposed to be a pattern (with one exception, which does not apply to your case - see perldoc -f split).

(2) You limit the number of fields to 2, but your example shows that there are 3 fields.

A minor issue is that your fields seemingly are not really delimited just by comma, but that there may be white spaces too around the comma, hence the delimiter pattern

Code
/\s*,\s*/

would be more appropriate.

Ronald


7stud
Enthusiast

Feb 24, 2010, 2:50 AM

Post #3 of 4 (487 views)
Re: [lokesha] Summing column value [In reply to] Can't Post

Your program is probably too complex for you at this time. What you should use is a hash of arrays. Each key will be the first part of each line, '20100221, abc_1', and the value will be an array into which you will push() the numbers at the end of each line. perl helps in this regard by performing a little magic--if you try to access a key in the hash that doesn't exist and then push a value onto its corresponding array, perl will automatically create the key and an empty array for you.

Then to print out the data in the format you want, you would step through each key/value of the hash, print the key, and then take the value, which is an array reference, and step through the array and total the array values, then print the total.

You'll need to read and understand perlreftut.


(This post was edited by 7stud on Feb 24, 2010, 3:59 AM)


Nila
User


Mar 5, 2010, 1:44 AM

Post #4 of 4 (455 views)
Re: [lokesha] Summing column value [In reply to] Can't Post

Try this code,


Code
use strict; 
use warnings;

my $inputfilename = "myfile.txt" ;

open(FH, $inputfilename ) or die "Can't open $inputfilename : $!";
my @columns ;
my %hash ;
while (my $line = <FH>) {
chomp($line);
$line =~ /^(.*?)\s*,(.*?)\s*,(.*?)\s*$/ ;
my $no = $1 ;
my $str = $2 ;
my $cnt = $3 ;
$str =~ s/^(.*?)_[0-9]*$/$1/;
$hash{$no}{$str}+=$cnt ;
}

close(FH);

foreach my $var ( keys%hash)
{

foreach my $str ( keys%{$hash{$var}})
{
print "$var,$str,$hash{$var}{$str}\n";
}
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives