×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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!


#!/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 ;


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

Hi

If I understand you correctly and the Added_*.txt files should contain a single number each, then the problems would be these :
  • 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.
  • 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.
Not problems but I would mention them anyway :
  • 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.
  • No idea what is the point of the 2nd pair of brackets ( { .. } )
  • 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.
  • You better close 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. )
  • In the open error messages you probably intended to use $!.

CODE --> Perl

#!/usr/local/bin/perl
use strict ;
use warnings ;

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)
{
    next if ($cell_file =~ m/^\./) ;
    next if $cell_file =~ m/^$added/;
    next unless (-f "$all_joinedf/$cell_file");
    my $comb = "$all_joinedf$cell_file" ;
    my $comb_2 = "$all_joinedf$added$cell_file" ;

    open my $comb_2_fh, '>', $comb_2 or die "cannot open $comb_2 $! \n" ;
    open (my $comb_fh, '<', $comb) or die "cannot open $comb $! \n" ;

    my $sum = 0 ;
    while (my $lna = <$comb_fh>)
    {
        chomp $lna ;
        $sum += $lna ;
    }
    print $comb_2_fh "$sum \n" ;

    close $comb_fh;
    close $comb_2_fh;
} 

Quote (Tester_V)

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

Feherke.
feherke.github.io

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

(OP)
Amazing! I came close but could not solve it.
Thank you!
And thank you for tutoring, I really appreciate both.
Tester_V.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close