Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to sum all numbers in in all files (separately) in a directory 1

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
0
0
US
It is my first post in the forum. Sorry if I'll do something wrong. I do not see "code" button, so I'll just paste the code here.


Hi,
I have number of files in a directory like this: Cell_01.txt, Cell_02.txt and so on...
Each file has number in a column, something like this:
12
06
1234
45
34567
And so on...
I need to open each file and add all the numbers in each file and then print the sum to a different file that will have the name (part of the name) of original file, something like this:
Added_Cell_01.txt, Added_Cell_02.txt and so on...
I created a script but the “sum” part of the script does not really work I wanted.
Thank you in advance!


[sup]#!/usr/local/bin/perl
use strict ;
use warnings ;
use diagnostics ;
use Time::piece ;
use Time::Seconds ;

use List::Util qw(sum) ;

my $sum = 0 ;
my $comb ;
my $comb_2 ;
my $added = 'Added_' ;


my $all_joinedf = "C:/02/E_Column/" ;
opendir (DIR, $all_joinedf) or die $!;
my @allcellfiles = readdir(DIR) ;
close (DIR) ;

foreach my $cell_file (@allcellfiles)
{
chomp $cell_file ;
{
next if ($cell_file =~ m/^\./) ;
next unless (-f "$all_joinedf/$cell_file");
$comb = "$all_joinedf$cell_file" ;
$comb_2 = "$all_joinedf$added$cell_file" ;
print "$comb \n" ;
print "$comb_2 \n" ;
# print "$cell_file \n" ;

# open my $comb_2_fh,'>',$comb_2 or die "cannot open $comb_2 !$ \n" ;
open (my $comb_fh,'<', $comb) or die "canot open file !$ \n" ;
while (my $lna = <$comb_fh>)
{
chomp $lna ;
$sum+= $lna ;
print "$lna \n" ;
print "$sum \n" ;
# print $comb_2_fh "$sum \n" ;
}
}
}

exit ;
[/sup]

 
Hi

If I understand you correctly and the Added_*.txt files should contain a single number each, then the problems would be these :
[ul]
[li]You initialize $sum only once at declaration before the loop. That way it will accumulate all input files' all numbers. Either move the declaration inside the outer loop or just initialize it each time with 0.[/li]
[li]The currently commented out writing to output file is inside the inner loop, so will write a running subtotal for each input number. Move it after the inner loop.[/li]
[/ul]
Not problems but I would mention them anyway :
[ul]
[li]Looks weird to cut off trailing newline from the input file name. They usually not contain newline, but if they do and you cut them off, you can be sure opening the file with altered name will fail.[/li]
[li]No idea what is the point of the 2[sup]nd[/sup] pair of brackets ( [tt]{[/tt] .. [tt]}[/tt] )[/li]
[li]As the output files are written to the same directory where the input files were, would be better to skip summing up the files created by previous runs of the script.[/li]
[li]You better [tt]close[/tt] the files. There is no guarantee that flushing the fresh file content will always happen successfully on script termination. As the available open file handles is limited by the system, you may run out of them by just opening and not closing. ( Though your script is not vulnerable to that as the file handle variables are local to the loop. )[/li]
[li]In the [tt]open[/tt] error messages you probably intended to use [tt]$![/tt].[/li]
[/ul]
Perl:
[gray]#!/usr/local/bin/perl[/gray]
[b]use[/b] strict [teal];[/teal]
[b]use[/b] warnings [teal];[/teal]

[b]my[/b] [navy]$added[/navy] [teal]=[/teal] [i][green]'Added_'[/green][/i] [teal];[/teal]

[b]my[/b] [navy]$all_joinedf[/navy] [teal]=[/teal] [i][green]"C:/02/E_Column/"[/green][/i] [teal];[/teal]
[b]opendir[/b] [teal]([/teal]DIR[teal],[/teal] [navy]$all_joinedf[/navy][teal])[/teal] or [b]die[/b] $[teal]!;[/teal]
[b]my[/b] [navy]@allcellfiles[/navy] [teal]=[/teal] [b]readdir[/b][teal]([/teal]DIR[teal]) ;[/teal]
[b]close[/b] [teal]([/teal]DIR[teal]) ;[/teal]

[b]foreach my[/b] [navy]$cell_file[/navy] [teal]([/teal][navy]@allcellfiles[/navy][teal])[/teal]
[teal]{[/teal]
    [b]next if[/b] [teal]([/teal][navy]$cell_file[/navy] [teal]=~[/teal] [b]m[/b][fuchsia]/^\./[/fuchsia][teal]) ;[/teal]
    [b]next if[/b] [navy]$cell_file[/navy] [teal]=~[/teal] [b]m[/b][fuchsia]/^[/fuchsia][navy]$added[/navy][fuchsia]/[/fuchsia][teal];[/teal]
    [b]next unless[/b] [teal](-[/teal]f [i][green]"$all_joinedf/$cell_file"[/green][/i][teal]);[/teal]
    [b]my[/b] [navy]$comb[/navy] [teal]=[/teal] [i][green]"$all_joinedf$cell_file"[/green][/i] [teal];[/teal]
    [b]my[/b] [navy]$comb_2[/navy] [teal]=[/teal] [i][green]"$all_joinedf$added$cell_file"[/green][/i] [teal];[/teal]

    [b]open my[/b] [navy]$comb_2_fh[/navy][teal],[/teal] [i][green]'>'[/green][/i][teal],[/teal] [navy]$comb_2[/navy] or [b]die[/b] [i][green]"cannot open $comb_2 $! \n"[/green][/i] [teal];[/teal]
    [b]open[/b] [teal]([/teal][b]my[/b] [navy]$comb_fh[/navy][teal],[/teal] [i][green]'<'[/green][/i][teal],[/teal] [navy]$comb[/navy][teal])[/teal] or [b]die[/b] [i][green]"cannot open $comb $! \n"[/green][/i] [teal];[/teal]

    [b]my[/b] [navy]$sum[/navy] [teal]=[/teal] [purple]0[/purple] [teal];[/teal]
    [b]while[/b] [teal]([/teal][b]my[/b] [navy]$lna[/navy] [teal]=[/teal] [i][green]<$comb_fh>[/green][/i][teal])[/teal]
    [teal]{[/teal]
        [b]chomp[/b] [navy]$lna[/navy] [teal];[/teal]
        [navy]$sum[/navy] [teal]+=[/teal] [navy]$lna[/navy] [teal];[/teal]
    [teal]}[/teal]
    [b]print[/b] [navy]$comb_2_fh[/navy] [i][green]"$sum \n"[/green][/i] [teal];[/teal]

    [b]close[/b] [navy]$comb_fh[/navy][teal];[/teal]
    [b]close[/b] [navy]$comb_2_fh[/navy][teal];[/teal]
[teal]}[/teal]

Tester_V said:
I do not see "code" button, so I'll just paste the code here.
You can just enclose the code between [tt][ignore]
Code:
[/ignore][/tt] .. [tt][ignore]
[/ignore][/tt] TGML tags to preserve its formatting. The toolbar's Code button also does only that.


Feherke.
feherke.github.io
 
Amazing! I came close but could not solve it.
Thank you!
And thank you for tutoring, I really appreciate both.
Tester_V.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top