GertMT has asked for the wisdom of the Perl Monks concerning the following question:
Data_1.txt#!/usr/bin/perl use strict; use vars qw !$file!; chdir '/test/' or die "Cannot change to /test: $!"; foreach $file ( readdir DH ) { } foreach $file ( glob "*.txt" ) { open FILE, '<', $file; my %saldi; while (<FILE>) { chomp; next unless $_; my @cellen = ( split /,/, )[ 3, 4 ]; next unless $cellen[0] && $cellen[1]; $saldi{ $cellen[0] } += $cellen[1]; } $file =~ m/^(\S+)\.txt/; print "$1\n"; foreach my $name ( keys %saldi ) { printf "\t$name\t" . "%16s", &big_money( $saldi{$name} ) . "\n +"; } close FILE; } closedir DH; sub big_money { #Learning Perl my $number = sprintf "%.2f", shift @_; #Add one comma each time though the do-nothing loop 1 while $number =~ s/^(-?\d+)(\d\d\d)/$1,$2/; #Put the dollar sign in the right place $number =~ s/^(-?)/$1/; $number; }
Data_2.txt394,eur,2006,D,18.20 394,eur,2006,D,22 394,eur,2006,C,25
494,eur,2006,C,25 494,eur,2006,D,79
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Make calculation with values from hash
by johngg (Canon) on Jan 02, 2007 at 19:35 UTC | |
by GertMT (Hermit) on Jan 02, 2007 at 20:20 UTC | |
by johngg (Canon) on Jan 02, 2007 at 20:54 UTC | |
|
Re: Make calculation with values from hash
by wfsp (Abbot) on Jan 02, 2007 at 18:33 UTC | |
by GertMT (Hermit) on Jan 02, 2007 at 19:08 UTC | |
by almut (Canon) on Jan 02, 2007 at 21:30 UTC | |
|
Re: Make calculation with values from hash
by GrandFather (Saint) on Jan 02, 2007 at 22:11 UTC | |
by GertMT (Hermit) on Jan 04, 2007 at 17:35 UTC |