ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all, I wrote a simple script to read in a list of transactions from my online banking in CSV format, scan it for keywords associated with budgets, and then provide the totals.
It works, but I'm wondering if there is a more natural way to do any of this with perl. Any suggestions appreciated!
Code:
#!/usr/bin/perl
use strict;
use warnings;
my %budgets;
my $budget;
my $transactions_file = $ARGV[0] or die "usage: calc.pl <transactions file>\n";
my %transactions;
my %totals;
my $first_transaction;
my $last_transaction;
$budgets{Groceries} = ["NO FRILL'S", "SOBEYS", "RABBA", "VALUMART", "LONGO"];
$budgets{Gas} = ["ESSO", "PETRO"];
$budgets{Savings} = ["<snip>"];
$budgets{"Eating out"} = ["subway", "wrap and grab", "bbq", "pizza", "cafe"];
$budgets{Alcohol} = ["lcbo", "beer"];
$budgets{"Drug store"} = ["shoppers", "rexal"];
$budgets{Office} = ["staples"];
$budgets{Walmart} = ["wal-mart"];
$budgets{Telus} = ["telus"];
$budgets{Rogers} = ["rogers"];
$budgets{Hydro} = ["tor hyd elec"];
$budgets{"Bank fees"} = ["fee", "interest"];
$budgets{"Visa - TD"} = ["tfr-to visa"];
$budgets{"Visa - Desjardins"} = ["visa desjard"];
$budgets{Pets} = ["pet"];
$budgets{Cash} = ["w/d"];
$budgets{Cheques} = ["chq"];
$budgets{"Canadian Tire"} = ["canadian tire"];
$budgets{Motorcycle} = ["cycle"];
open TRANSACTIONS, "$transactions_file" or die "can't open $transactions_file ($!)\n";
my @all_transactions = <TRANSACTIONS>;
close (TRANSACTIONS);
if ($all_transactions[0] =~ /^([^,]*),/) {
$first_transaction = $1;
}
if ($all_transactions[@all_transactions - 1] =~ /^([^,]*),/) {
$last_transaction = $1;
}
foreach $budget (keys %budgets) {
my @t;
foreach my $keyword (@{$budgets{$budget}}) {
@t = (@t, grep /$keyword/i, @all_transactions);
}
$transactions{$budget} = \@t;
$totals{$budget} = 0;
foreach (@{$transactions{$budget}}) {
my @bits = split /,/;
if ($bits[2]) {
$totals{$budget} += $bits[2];
}
if ($bits[3]) {
$totals{$budget} -= $bits[3];
}
}
$totals{$budget} = '$' . sprintf "%.2f", $totals{$budget};
}
$" = "";
print "First transaction: $first_transaction, Last transaction: $last_transaction\n\n";
print "Raw transactions:";
foreach $budget (sort keys %transactions) {
$" = "\t";
print "\n$budget:\n\t@{$transactions{$budget}}";
}
print "\nTotals:\n";
foreach $budget (sort keys %totals) {
print "\t$budget: $totals{$budget}\n";
}
I was aware that I could have written it that way, but find the other way a bit more readable.
Quote:
Code:
open TRANSACTIONS
- as a habit don't use global filehandles; "$transactions_file" is no better than $transactions_file, better write
Code:
open(my $transcations_fh, '<', $transactions_file) or die "can't open '$transactions_file' file for reading ($!)\n";
.
Oh, thank you. I found the code for reading in a file through google, and didn't know that I was using a global filehandle.
So using your example, I can do something like this instead:
Code:
open(my $transcations_fh, '<', $transactions_file) or die "can't open '$transactions_file' file for reading ($!)\n";
my @all_transactions = <$transactions_fh>;
I was aware that I could have written it that way, but find the other way a bit more readable.
Oh, thank you. I found the code for reading in a file through google, and didn't know that I was using a global filehandle.
So using your example, I can do something like this instead:
Code:
open(my $transcations_fh, '<', $transactions_file) or die "can't open '$transactions_file' file for reading ($!)\n";
my @all_transactions = <$transactions_fh>;
Is that correct?
Thanks!
Readability is always debatable; my code has less '=' which, I think, makes it better.
The idiom you've used sometimes must be used - when there is inter-key dependency, e.g.
Code:
my %hash;
$hash{number} = 3;
$hash{increased_by_2_number} = $hash{number} + 2;
.
Yes, you are dealing with the newly introduced $transcations_fh correctly.
You can have your whole hash in a separate file:
Code:
sergei@amdam2:~/junk> cat -n data_importer.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 my $hash_ref = require "./data.prl";
6
7 foreach my $key(sort keys %{$hash_ref})
8 {
9 my $value = ${$hash_ref}{$key};
10 warn "\$key=$key, |\$value|=|$value|"; # '|'s show the limits for clarity
11 }
12
sergei@amdam2:~/junk> cat -n data.prl
1 use strict;
2
3
4 # anonymous hash reference to be exported
5 {
6 foo => "whatever you like",
7 bar => "and something you need"
8 };
sergei@amdam2:~/junk> ./data_importer.pl
$key=bar, |$value|=|and something you need| at ./data_importer.pl line 10.
$key=foo, |$value|=|whatever you like| at ./data_importer.pl line 10.
sergei@amdam2:~/junk>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.