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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have to do a group by and sum two columns, I am able to do only for two columns. For the below code, I got the help from Monks only. I need another help to group by and sum two columns. I have grouped by using col2, and sum col6 and col7

  • Input file
  • col1,col2,col3,col4,col4,col5,col6,col7
  • 1234,GP,20170715,0,V,97517,24,0.6
  • 5678,Pack,20170715,0,V,97516,88,1.8
  • 1234,GP,20170715,0,V,97517,22,0.6
  • 5678,Pack,20170715,0,V,97517,183,3.9
  • 1234,PRS,20170715,0,S,97517,261,5.4
  • 5678,PRS,20170715,0,M,97517,36,0.9
  • output should be:
  • GP,46,1.2
  • Pack,271,5.7
  • PRS,297,6.3
below is the code I have tried.
#!/usr/bin/perl #use strict; #use warnings; use feature 'say'; #my %hash; print "Please input the Service Name(V/M/S/ALL):"; my $sinput = <STDIN>; chomp $sinput; print "Please input On/Off/ALL:"; my $chcat= <STDIN>; chomp $chcat; print "Start Date:"; chomp($sdate=<>); print "End Date:"; chomp($edate=<>); $file = '/var/tmp/CA_scripts/1_onnet_offnet/Onnet_offnet_repository.cs +v'; open (my $fh, '>>', 'C_n_r_'.$sinput.'.csv'); unless(open FILE, $file) { die "\nUnable to open the file\n"; } while (my $line = <FILE>) { my ($col1,$col2,$col3,$col4,$col5,$col6,$col7,$col8) = split /,/, +$line; #my $col21 = $class; if ($sdate >= $col3 && $edate <= $col3 && $sinput eq $col5 && $chcat e +q "On" && ($col6 =~ /^\Q97517\E/ || $col6 =~ /^\Q97516\E/)) { $hash{"$col2"} += $col8; #$hash{"$col2"} += $col6; } elsif ($sdate >= $col3 && $edate <= $col3 && $sinput eq $col5 && $chca +t eq "Off" && $col6 =~ /^\Q97577\E/) { $hash{"$col2"} += $col8; } elsif ($sdate >= $col3 && $edate <= $col3 && $sinput eq "ALL" && $chc +at eq "ALL") { $hash{"$col2"} += $col8; } } foreach my $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) { printf $fh "%s %1f\n", $key, $hash{$key};

In reply to group by and sum for two columns by gowthamvels

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-16 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found