Hello,

I'm attempting to remove the commas from this directory listing (provided to me as a textfile).

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
My task is to parse out the filesizes and filenames, determine the averagefilesize, and provide a formatted output as shown below.
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";

In reply to How to Remove Commas ? by allendevans

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.