piggybank/brother.csv
piggybank/sister.csv
piggybank/mother.csv
piggybank/test.csv
####
deposit;1
beer;-30
beer;-20
won bet;200
##
##
deposit;1000
buy coffee;-20
buy lunch;-30
new shoes;-500
deposit;20
##
##
deposit;50
deposit;30
deposit;300
small car accident;-550
##
##
#!/usr/bin/env perl
use strict;
use warnings;
# get a list of all CSV files in the piggybank directory
my @fnames = glob('piggybank/*.csv');
foreach my $fname (@fnames) {
if(!-f $fname) {
print STDERR "$fname is not a file!\n";
next;
}
print "Found data file $fname\n";
}
##
##
#!/usr/bin/env perl
use strict;
use warnings;
# get a list of all CSV files in the piggybank directory
my @fnames = glob('piggybank/*.csv');
foreach my $fname (@fnames) {
if(!-f $fname) {
print STDERR "$fname is not a file!\n";
next;
}
readAccount($fname);
}
sub readAccount {
my ($fname) = @_;
open(my $fh, "<", $fname) or die($!);
foreach my $line (<$fh>) {
chomp $line;
print $fname, ': ', $line, "\n";
}
close $fh;
}
##
##
sometext;value
##
##
sub readAccount {
my ($fname) = @_;
my $balance = 0;
open(my $fh, "<", $fname) or die($!);
foreach my $line (<$fh>) {
chomp $line;
if($line =~ /(.+)\;(.+)/) {
$balance += $2;
}
}
close $fh;
print "$fname balance: $balance\n";
}
##
##
#!/usr/bin/env perl
use strict;
use warnings;
# get a list of all CSV files in the piggybank directory
my @fnames = glob('piggybank/*.csv');
my $total = 0;
foreach my $fname (@fnames) {
if(!-f $fname) {
print STDERR "$fname is not a file!\n";
next;
}
$total += readAccount($fname);
}
print "Money left in the piggybank: $total\n";
sub readAccount {
my ($fname) = @_;
my $balance = 0;
open(my $fh, "<", $fname) or die($!);
foreach my $line (<$fh>) {
chomp $line;
if($line =~ /(.+)\;(.+)/) {
$balance += $2;
}
}
close $fh;
print "$fname balance: $balance\n";
return $balance;
}
##
##
piggybank/brother.csv balance: 151
piggybank/test.csv is not a file!
piggybank/mother.csv balance: -170
piggybank/sister.csv balance: 470
Money left in the piggybank: 451