0

I am trying to write code to add up revenue on sales based on the month of a particular year and output total per month per year, something with my code is wrong though and I am just stuck now.

use strict;
use warnings;
my @newPlans = `cat plansSold.txt`;
my @oldPlans = `cat plansCancelled.txt`;
my %amounts;
for(@newPlans){
    chomp;
    my ($price,$billPeriod,$date) = (split /\;/, $_ )[4,5,7];
    my $montly = $price/$billPeriod;
    my ($f,$l) = (split /\-/, $date)[0,1];
    $montly = (split /\./, $montly)[0];
    $date = "$f\-$l";
    $amounts{$date} += $montly;
}
for my $date (sort keys %amounts) {
    print '.';
    print "$amounts{$date} : $_\n" for (sort keys %{ $amounts{$date} });
}

$price,$billPeriod,$date Are as below.

7.95,1,2012-04
9.95,1,2012-06
19.95,12,2012-06
19.95,1,2014-02
12.95,3,2013-03
19.95,1,2014-01
18.95,1,2012-12
18.95,6,2012-12
18.95,1,2012-05

And the error:

Can't use string ("6177") as a HASH ref while "strict refs" in use at revenueHistory.pl line 28.

Whis is this line

for my $date (sort keys %amounts) {

I cant seem to figure out how to fix it.

ThatGuy
  • 303
  • 2
  • 14
  • 1
    Please don't shell out to a new process just to read the contents of a file – Borodin Jun 06 '14 at 00:15
  • 1
    Please show the contents of your input files – Borodin Jun 06 '14 at 00:24
  • Is this a program that you've inherited from someone else? If so then you should say so. That way you would avoid a lot of the flack that the original author deserved – Borodin Jun 06 '14 at 00:25

2 Answers2

2

This line

print "$amounts{$date} : $_\n" for (sort keys %{ $amounts{$date} });

is trying to treat a numeric total in $amounts{$date} as a hash reference, and it isn't. You've been totaling up a running total, and you have a number, but now you want it to be a hash reference, and that doesn't make sense.

What is it you're actually trying to do?

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
-2

Got it

use strict;
use warnings;
my @newPlans = `cat plansSold.txt`;
my @oldPlans = `cat plansCancelled.txt`;
my %amounts;
my $hold = 'tt';
for(@newPlans){
    chomp;
    my ($price,$billPeriod,$date) = (split /\;/, $_ )[4,5,7];
    my $montly = $price/$billPeriod;
    my ($f,$l) = (split /\-/, $date)[0,1];
    $montly = (split /\./, $montly)[0];
    $date = "$f\-$l";
    $amounts{$date}{$date} += $montly;
    #print "$price,$billPeriod,$date\n";
}
for my $date (sort keys %amounts) {
    print '.';
    print "$amounts{$date}{$date} : $_\n" for (sort keys %{ $amounts{$date}});
}
ThatGuy
  • 303
  • 2
  • 14
  • 1
    That did not fix your code, you just added another error to cancel out your first error. What is wrong with `sort keys %amounts`? And do you really need to loop twice? – TLP Jun 05 '14 at 22:27
  • Also... even if it had fixed your code, this is just a blind dump of code. Don't forget there are newbies on this site that would like to know how your change would have fixed it... a bit more explanation is always a good thing :) – Taryn East Jun 06 '14 at 00:19
  • @ThatGuy: Please show the contents of your input files – Borodin Jun 06 '14 at 00:23
  • Yea, I got this bit from another guy to be honest. I used it for something a little different and modification isn't working. The contents of the input file are above in the original post, essentially it is a price,billingperiod,date...I want the monthly dollar amount from each row, so if a price is 1200 the value is 120. I then want to take every dollar amount and add it up based on the month. So 2014-01 will show as 2014-01 : $totalDollars. It seems to be adding wrong though as excel shows a different value than the script is spitting out. – ThatGuy Jun 06 '14 at 12:40