My average

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Albert Cardenas
    New Member
    • Mar 2012
    • 3

    My average

    I'm trying to figure this out but my $average is not calculating right.

    My $filesize is supposed to add into my $totalFileSize after every iteration. Then $totalFileSize is supposed to be divided by $count and that NUMBER is supposed to equal $average.

    Here is my code and the output I get.

    -----------------------------------------------------------
    Code:
    #!/usr/bin/perl
    open (ALBERT_LAB, "Lab3.txt") or die ("Cannot open file");
    
    while(<ALBERT_LAB>)
    {    
        chomp;
        ($date, $time, $ampm, $filesize, $filename) = split(" ");
        print ("$filesize\t\t$filename\n");
        $count++;
        $totalFileSize+=$filesize;
    }
    
    $average = $totalFileSize/$count;
    print("\nTotal Files: $count \tAverage file size: $average\n");
     
    close(ALBERT_LAB);
    -----------------------------------------------------

    OUTPUT
    Code:
    1,572,727,014		IN11-135.E01
    1,223		IN11-135.E01.txt
    1,572,694,696		IN11-135.E02
    1,572,740,428		IN11-135.E03
    1,572,785,002		IN11-135.E04
    1,572,696,748		IN11-135.E05
    1,572,745,871		IN11-135.E06
    1,572,737,726		IN11-135.E07
    1,572,785,459		IN11-135.E08
    1,572,777,135		IN11-135.E09
    1,572,751,922		IN11-135.E10
    1,572,684,462		IN11-135.E11
    1,556,456,660		IN11-135.E12
    
    Total Files: 13 	Average file size: 1
    Last edited by Frinavale; Mar 21 '12, 01:43 PM. Reason: Added code tags.
  • chorny
    Recognized Expert New Member
    • Jan 2008
    • 80

    #2
    you need to remove comma.
    Code:
    $filesize =~ s/,//;

    Comment

    • Albert Cardenas
      New Member
      • Mar 2012
      • 3

      #3
      Excuse my ignorance but I literally started learning Perl 2 days ago just for this one assignment I have to do and I have no idea about your comment?

      Are you saying I need to remove a specific comma from my code? or the comma from the line of code you gave me?

      secondly, I dunno if I'm supposed to replace a certain line of code with the code you gave me or if I'm supposed to add it to the lab?

      Sorry, I'm trying to understand and I've tried many options with what you gave me but it keeps returning the same value...

      thanks,
      -Albert Cardenas

      Comment

      • chorny
        Recognized Expert New Member
        • Jan 2008
        • 80

        #4
        You have file sizes in form of "1,572,694,696" . In programming comma is not accepted as part of number. You should remove it. It is best to do with help of regular expression. You need to do it for every file size. So in you cycle of reading lines of file add code that I provided. Insert it before line that uses $filesize for addition. You can insert it before "print", so you will be able to see modified value of $filesize.

        See http://learn.perl.org/ for Perl learning links, especially "Beginning Perl" book: http://learn.perl.org/books/beginning-perl/

        Comment

        • Albert Cardenas
          New Member
          • Mar 2012
          • 3

          #5
          Thank you! I appreciate the help. Code works great now! I just had to modify and add the "g" at the end

          [CODE=perl]$filesize =~ s/,//g;[/CODE]

          So now it looks like

          [CODE=perl]#!/usr/bin/perl
          open (ALBERT_LAB, "Lab3.txt") or die ("Cannot open file");
          while(<ALBERT_L AB>)
          {
          chomp;
          ($date, $time, $ampm, $filesize, $filename) = split(" ");
          print ("$filesize\t\t $filename\n");
          $filesize =~ s/,//g;
          $count++;
          $totalFileSize+ =$filesize;
          }
          $average = $totalFileSize/$count;
          print("\nTotal Files: $count \tAverage file size: $average\n");

          close(ALBERT_LA B);[/CODE]

          With the output

          [CODE=perl]1,572,727,014 IN11-135.E01
          1,223 IN11-135.E01.txt
          1,572,694,696 IN11-135.E02
          1,572,740,428 IN11-135.E03
          1,572,785,002 IN11-135.E04
          1,572,696,748 IN11-135.E05
          1,572,745,871 IN11-135.E06
          1,572,737,726 IN11-135.E07
          1,572,785,459 IN11-135.E08
          1,572,777,135 IN11-135.E09
          1,572,751,922 IN11-135.E10
          1,572,684,462 IN11-135.E11
          1,556,456,660 IN11-135.E12

          Total Files: 13 Average file size: 1450506488.1538 5[/CODE]

          Thank you, again Chomp

          -Albert Cardenas

          Comment

          Working...