in reply to Re^3: Solution for your prof
in thread <> diamond Operator

wow guys, I really feel good know, actually I am learning lot of stuff, I've been awake from 7 am. and learning pearl all day long. Here is the script tha I modified. The script should do the following: copy all the files that were entered in command line to a folder lab06d which is already created. and add a "b" at the end of the file to tell that is a backup. also it should print the total value added from all the files. but I don't thing is adding the value from all the files together. here is the script again:

#!/perl/bin/perl
#
# Author: L.H
# Script Name: lab6-1
# Date Written: March 15, 2005
# Purpose: Basic I/0
#
#
print "L.H\n";
print "Starting Script\n\n";
print "Filename\tValue\t(print heading)\n";

use File::Copy;
use strict;
my $total;
foreach my $file (@ARGV)
{
open(FILE, "$file");

my $sum = 0;
while (<FILE>)
{
chomp();
$sum+=$_;
print "$file $_\n";
$total+=$sum;
}
print "Total value for the $file : $sum\n";
close( FILE );
}
print "Total value accumulated from all the files
is:$total";

foreach my $file (@ARGV)
{
my $newfile=$file . "b";
copy ($file, "lab06d\\$newfile");
}

Replies are listed 'Best First'.
Re^5: Solution for your prof
by dragonchild (Archbishop) on Apr 08, 2005 at 01:06 UTC
    The script should do the following: copy all the files that were entered in command line to a folder lab06d which is already created. and add a "b" at the end of the file to tell that is a backup. also it should print the total value added from all the files. but I don't thing is adding the value from all the files together.

    Does it do those things? Have you tested it out? It looks good, but the only way to know is by actual testing. Good luck! :-)

Re^5: Solution for your prof
by starbolin (Hermit) on Apr 08, 2005 at 03:31 UTC

    I did this:

    > cat - >1.pl 1 > > cat - >2.pl 2 > > cat - >3.pl 3 > > perl foo.pl ?.pl 1.pl 1 Total value for the 1.pl : 1 2.pl 2 Total value for the 2.pl : 2 3.pl 3 Total value for the 3.pl : 3 Total value accumulated from all the files is:6>

    Which is the correct total. So either you didn't test it or you didn't specify what you though the errant behavior was. Either will get you down votes.

    If you format your code as follows it will be more readable to you and others. You can use ether tabs or spaces, Perl doesn't care.

    foreach my $file (@ARGV) { open(FILE, "$file"); my $sum = 0; while (<FILE>) { chomp(); $sum+=$_; print "$file $_\n"; $total+=$sum; } print "Total value for the $file : $sum\n"; close( FILE ); }

    Also; you already foreach through the files, why not do the copy in the same loop?

    Please when you post code place <code> before and </code> after. These html tags put the code into the pretty little boxes you see in other posts. It also keeps the code from fouling up the browsers for those that read your posts. Please read Writeup Formatting Tips again and take it to heart.


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}