perl hash - data processing

S

shree

I have a data set that looks like this. It basically shows members
added to an organization on a monthly basis.

2007-01|member1
2007-01|member2
2007-01|member3
2007-02|member4
2007-03|member5
2007-03|member6

I wrote the following script to get monthly counts.

#!/usr/bin/perl
my %hash;
my $File_In = "Members.txt";
open (DATA, $File_In) or die "Cannot open File_In $!";
while( <DATA> ){
my($yyyy_mm, $member_name) = split /\|/;
$hash{$yyyy_mm}++;
}
print "yyyy-mm|members added\n";
foreach (sort keys %hash) {
print "$_|$hash{$_}\n";
}

yyyy-mm|members added
2007-01|3
2007-02|1
2007-03|2

Now I'm asked to add 2 more fields in the output. One is running
cumulative total and another is percentage increase over the
preceeding month. I'm not sure how to this and would appreciate any
tips. I have listed a mockup of the results - done in Excel for
illustration.

yyyy-mm|members added|cumulative total|% increase over last month
2007-01|3|3|-
2007-02|1|4|33.33%
2007-03|2|6|50.00%

Thanks,
Shree
 
P

Paul Lalli

I have a data set that looks like this. It basically shows members
added to an organization on a monthly basis.

2007-01|member1
2007-01|member2
2007-01|member3
2007-02|member4
2007-03|member5
2007-03|member6

I wrote the following script to get monthly counts.

#!/usr/bin/perl
my %hash;
my $File_In = "Members.txt";
open (DATA, $File_In) or die "Cannot open File_In $!";
while( <DATA> ){
my($yyyy_mm, $member_name) = split /\|/;
$hash{$yyyy_mm}++;}

print "yyyy-mm|members added\n";
foreach (sort keys %hash) {
print "$_|$hash{$_}\n";

}

yyyy-mm|members added
2007-01|3
2007-02|1
2007-03|2

Now I'm asked to add 2 more fields in the output. One is running
cumulative total and another is percentage increase over the
preceeding month. I'm not sure how to this and would appreciate any
tips. I have listed a mockup of the results - done in Excel for
illustration.

yyyy-mm|members added|cumulative total|% increase over last month
2007-01|3|3|-
2007-02|1|4|33.33%
2007-03|2|6|50.00%

Fairly straightforward...

#!/usr/bin/env perl
use strict;
use warnings;

my %hash;
while( <DATA> ){
my($yyyy_mm, $member_name) = split /\|/;
$hash{$yyyy_mm}++;
}

my $total = 0;
print "yyyy-mm|members added|cum. total|increase\n";
foreach (sort keys %hash) {
my $incr = $total
? sprintf("%.2f%%", 100 * $hash{$_} / $total)
: '-';
$total += $hash{$_};
print "$_|$hash{$_}|$total|$incr\n";
}

__DATA__
2007-01|member1
2007-01|member2
2007-01|member3
2007-02|member4
2007-03|member5
2007-03|member6

Paul Lalli
 
S

shree

my $total = 0;
print "yyyy-mm|members added|cum. total|increase\n";
foreach (sort keys %hash) {
my $incr = $total
? sprintf("%.2f%%", 100 * $hash{$_} / $total)
: '-';
$total += $hash{$_};
print "$_|$hash{$_}|$total|$incr\n";

}
</snip>

Dear Paul,

Thanks a million..this worked exactly as intended.

Shree
 
S

shree

Paul Lalli schreef:






Please be more efficient on the quoting next time.

--
Affijn, Ruud

"Gewoon is een tijger."- Hide quoted text -

- Show quoted text -

Hello Dr.Ruud,

Can you show how to quote for future?

Thanks,
Shree
 
J

Joe Smith

shree said:
Hello Dr.Ruud,

Can you show how to quote for future?

Thanks,
Shree

Darn it, Shree, did you _HAVE_ to quote all 49 lines verbatim to
ask that question? Sheesh! You're supposed to delete lines until
only the relevant parts are left. Like I have just done.
It's just common sense.

-Joe
 
P

Paul Lalli

Please be more efficient on the quoting next time.

Are you seriously that bored? In this case, the entire OP's message
was relevant. Situation, existing code, data, and desired effect. I
quoted what was necessary.

Paul Lalli
 
D

Dr.Ruud

shree schreef:
Hello Dr.Ruud,
Can you show how to quote for future?

No problem. I would attribute and quote like the first nine lines inside
the --- below.

------------------------------------------
shree said:
[input as in the DATA-section below]
[desired output:]
yyyy-mm|members added|cumulative total|% increase
2007-01|3|3|-
2007-02|1|4|33.33%
2007-03|2|6|50.00%

#!/usr/bin/perl
use strict;
use warnings;

[more code]

__DATA__
2007-01|member1
2007-01|member2
2007-01|member3
2007-02|member4
2007-03|member5
2007-03|member6
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top