allendevans has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
I'm attempting to remove the commas from this directory listing (provided to me as a textfile).
My task is to parse out the filesizes and filenames, determine the averagefilesize, and provide a formatted output as shown below.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
100 File name 1 1000 File name 2 100 File name 3 Total files: 3 Average file size: 400 bytes
I tried the following perl code (most recent script) without success. I've tried using both scalars and arrays. Reading the information is simple enough, it's the embedded commas that cause problems. I've not found a "search and replace" command to replace the commas with "nothing", allowing direct numeric manipulation of the scalar variables. I dabbled with arrays, but that got hairy fast.
Any assistance will be greatly appreciated.
#!/usr/bin/perl # author: Allen Evans # title: Lab3 assignemnt # purpose: Calculate average file size (avfsz) # open Lab3.txt file # ================== open(INPUT, "./Lab3.txt") or die "Can't open Lab3.txt\n"; # declare variables # ================= # unnecessary in perl, but good coding habits are tough to make $date = ""; $time = ""; $ampm = ""; $filesize = 0; $filename = ""; $totalfilesize = 0; $averagefilesize = 0; $i = 0; # while not eof # ============= while ($line = <INPUT>) { ($date, $time, $ampm, $filesize, $filename) = split(" ", $line); # determine $filesize # =================== # print "filesize: $filesize\n"; # debugging code $totalfilesize += $filesize; # create $totalfilesize va +riable # print "totalfilesize: $totalfilesize\n"; # debugging code # remove commas from $filesize string # =================================== # NSTR # place holder # convert $filestize string to numeric # ==================================== # NSTR # place holder # calculate totalfilesize and average, increment i # ================================================ $i += 1; # increment $i (denom) # print "i: $i\n"; # debugging code $averagefilesize = $totalfilesize / $i; # calculate $av +eragefilesize # print "averagefilesize: $averagefilesize\n\n"; # debugging code } # print output # ============ print "$filesize $filename\n"; # print output to screenn print "Total Files: $i Avg Size: $averagefilesize\n\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How to Remove Commas ?
by roboticus (Chancellor) on Mar 27, 2012 at 12:22 UTC | |
Re: How to Remove Commas ?
by moritz (Cardinal) on Mar 27, 2012 at 12:20 UTC | |
by Anonymous Monk on Mar 27, 2012 at 12:40 UTC | |
Re: How to Remove Commas ?
by ramlight (Friar) on Mar 27, 2012 at 12:22 UTC | |
by allendevans (Initiate) on Mar 27, 2012 at 13:05 UTC | |
by johngg (Canon) on Mar 27, 2012 at 13:53 UTC | |
by tobyink (Canon) on Mar 27, 2012 at 14:06 UTC | |
Re: How to Remove Commas ?
by ww (Archbishop) on Mar 27, 2012 at 12:23 UTC | |
Re: How to Remove Commas ?
by flexvault (Monsignor) on Mar 27, 2012 at 13:16 UTC | |
by allendevans (Initiate) on Mar 27, 2012 at 13:26 UTC | |
by ww (Archbishop) on Mar 27, 2012 at 14:11 UTC | |
by tobyink (Canon) on Mar 27, 2012 at 16:10 UTC | |
by flexvault (Monsignor) on Mar 27, 2012 at 13:58 UTC | |
Re: How to Remove Commas ?
by GrandFather (Saint) on Mar 27, 2012 at 20:24 UTC | |
Re: How to Remove Commas ?
by Anonymous Monk on Mar 27, 2012 at 12:32 UTC |