in reply to How to Remove Commas ?

Don't comment what the code tells you it does no matter how wonderfull the code seems to be. You can almost guarantee that if you choose good variable names and write clean code your tutor will have no trouble understanding it without comments. It's even possible the tutor may actually be able to see the code instead of being overwelmed by comments.

Always use strictures (use strict; use warnings;). That on its own would pick up at least one set of bugs in your code. Declare your variables in the smallest scope that you can (other replies have already said that so it must be true) - that with strictures would pick up another bug for you.

Use the three parameter version of open and lexical file handles (declared with my).

Use indentation to reflect the structure of your code - indent one level for each nested block.

The following code illustrates these points. Note that there are several bugs left in this code that will show up due to strictures when you run it. Do not put your name on this code and hand it in unless you want low marks!

#!/usr/bin/perl use strict; use warnings; my $file = <<'FILE'; Directory of E:\DOM\D2\HDD\IN11-135 04/04/2011 11:29 PM <DIR> . 04/04/2011 11:29 PM <DIR> .. 11/21/2010 05:02 AM 1,572,727,014 IN11-135.E01 11/21/2010 05:02 AM 1,223 IN11-135.E01.txt 11/21/2010 05:02 AM 1,572,694,696 IN11-135.E02 11/21/2010 05:02 AM 1,572,740,428 IN11-135.E03 11/21/2010 05:02 AM 1,572,785,002 IN11-135.E04 11/21/2010 05:02 AM 1,572,696,748 IN11-135.E05 11/21/2010 05:02 AM 1,572,745,871 IN11-135.E06 11/21/2010 05:02 AM 1,572,737,726 IN11-135.E07 11/21/2010 05:02 AM 1,572,785,459 IN11-135.E08 11/21/2010 05:02 AM 1,572,777,135 IN11-135.E09 11/21/2010 05:02 AM 1,572,751,922 IN11-135.E10 11/21/2010 05:02 AM 1,572,684,462 IN11-135.E11 11/21/2010 05:02 AM 1,556,456,660 IN11-135.E12 13 File(s) 18,856,584,346 bytes FILE open my $in, '<', \$file or die "Can't open Lab3.txt\n"; my $numFiles; my $totalFileSize = 0; while (my $line = <$in>) { my ($date, $time, $ampm, $filesize, $filename) = split(" ", $line) +; $filesize =~ y/,//d; $totalFileSize += $filesize; ++$numFiles; } #printf "%6d %s\n", $filesize, $filename; if ($numFiles) { printf "Total Files: %d, avg Size: %d\n\n", $numFiles, $totalFileS +ize / $numFiles; }
True laziness is hard work