|
|
 |
 |
 |
 |
Perl Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
perl hash - data processing
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
On Jun 2, 8:09 pm, shree <srigowr@hotmail.com> wrote:
> 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
<snip> > 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
Paul Lalli schreef:
> On Jun 2, 8:09 pm, shree <srigowr @hotmail.com> wrote: >> 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%
Please be more efficient on the quoting next time. -- Affijn, Ruud "Gewoon is een tijger."
On Jun 3, 7:52 am, "Dr.Ruud" <rvtol+n@isolution.nl> wrote:
> Paul Lalli schreef: > > On Jun 2, 8:09 pm, shree <srigowr@hotmail.com> wrote: > >> 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% > Please be more efficient on the quoting next time. > -- > Affijn, Ruud > "Gewoon is een tijger."- Hide quoted text - >
Hello Dr.Ruud, Can you show how to quote for future? Thanks, Shree
shree wrote: > On Jun 3, 7:52 am, "Dr.Ruud" <rvtol+n @isolution.nl> wrote: > 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
On Jun 3, 8:52 am, "Dr.Ruud" <rvtol+n@isolution.nl> wrote: > 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
Paul Lalli schreef: > I quoted what was necessary.
Not. -- Affijn, Ruud "Gewoon is een tijger."
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 wrote: > [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 ------------------------------------------ -- Affijn, Ruud "Gewoon is een tijger."
|
 |
 |
 |
 |
|