473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.

-----------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. open (ALBERT_LAB, "Lab3.txt") or die ("Cannot open file");
  3.  
  4. while(<ALBERT_LAB>)
  5. {    
  6.     chomp;
  7.     ($date, $time, $ampm, $filesize, $filename) = split(" ");
  8.     print ("$filesize\t\t$filename\n");
  9.     $count++;
  10.     $totalFileSize+=$filesize;
  11. }
  12.  
  13. $average = $totalFileSize/$count;
  14. print("\nTotal Files: $count \tAverage file size: $average\n");
  15.  
  16. close(ALBERT_LAB);
-----------------------------------------------------

OUTPUT
Expand|Select|Wrap|Line Numbers
  1. 1,572,727,014        IN11-135.E01
  2. 1,223        IN11-135.E01.txt
  3. 1,572,694,696        IN11-135.E02
  4. 1,572,740,428        IN11-135.E03
  5. 1,572,785,002        IN11-135.E04
  6. 1,572,696,748        IN11-135.E05
  7. 1,572,745,871        IN11-135.E06
  8. 1,572,737,726        IN11-135.E07
  9. 1,572,785,459        IN11-135.E08
  10. 1,572,777,135        IN11-135.E09
  11. 1,572,751,922        IN11-135.E10
  12. 1,572,684,462        IN11-135.E11
  13. 1,556,456,660        IN11-135.E12
  14.  
  15. Total Files: 13     Average file size: 1
Mar 21 '12 #1

✓ answered by chorny

you need to remove comma.
Expand|Select|Wrap|Line Numbers
  1. $filesize =~ s/,//;

4 1805
chorny
80 Expert
you need to remove comma.
Expand|Select|Wrap|Line Numbers
  1. $filesize =~ s/,//;
Mar 21 '12 #2
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
Mar 21 '12 #3
chorny
80 Expert
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/
Mar 21 '12 #4
Thank you! I appreciate the help. Code works great now! I just had to modify and add the "g" at the end

Expand|Select|Wrap|Line Numbers
  1. $filesize =~ s/,//g;
So now it looks like

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. open (ALBERT_LAB, "Lab3.txt") or die ("Cannot open file");
  3. while(<ALBERT_LAB>)
  4. {    
  5.     chomp;
  6.     ($date, $time, $ampm, $filesize, $filename) = split(" ");
  7.     print ("$filesize\t\t$filename\n");
  8.     $filesize =~ s/,//g;
  9.     $count++;
  10.     $totalFileSize+=$filesize;
  11. }
  12. $average = $totalFileSize/$count;
  13. print("\nTotal Files: $count \tAverage file size: $average\n");
  14.  
  15. close(ALBERT_LAB);
With the output

Expand|Select|Wrap|Line Numbers
  1. 1,572,727,014        IN11-135.E01
  2. 1,223        IN11-135.E01.txt
  3. 1,572,694,696        IN11-135.E02
  4. 1,572,740,428        IN11-135.E03
  5. 1,572,785,002        IN11-135.E04
  6. 1,572,696,748        IN11-135.E05
  7. 1,572,745,871        IN11-135.E06
  8. 1,572,737,726        IN11-135.E07
  9. 1,572,785,459        IN11-135.E08
  10. 1,572,777,135        IN11-135.E09
  11. 1,572,751,922        IN11-135.E10
  12. 1,572,684,462        IN11-135.E11
  13. 1,556,456,660        IN11-135.E12
  14.  
  15. Total Files: 13     Average file size: 1450506488.15385
Thank you, again Chomp

-Albert Cardenas
Mar 21 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sara | last post by:
I am trying to find the average number of products of the set that is on rows...dynamically. For example, if I had states on rows and was looking at the average number of products, I would see...
6
by: J | last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller program. Heres's what is supposed to happen: Roll 2 6-sided dies. Add rolls together put total in rolls(d6total). Display...
4
by: Gary | last post by:
Hi, I have a temperature conversion program down pat, but I was told to add an average, meaning, i need to get the average temperature for as many times as it was entered. i do not know where to...
3
by: C++Geek | last post by:
I need to get this program to average the salaries. What am I doing wrong? //Program to read in employee data and calculate the average salaries of the emplyees.
3
by: simpleeshelbee | last post by:
Hey guys, This is my second post and is URGENT!!!! My final assignment is due tonite for class and I have no idea how to write this program right! I am supposed to write a program that uses a...
3
by: mochatrpl | last post by:
I am looking for a way to make a query / report display the running average for total dollars. I have already set up a query to provide totals dollars per day from which a report graphly shows...
3
by: Salad | last post by:
http://www.mathwords.com/w/weighted_average.htm At the above link gives an example of a weighted average. It uses the following example: Grades are often computed using a weighted average....
3
by: vileoxidation | last post by:
Hello, and thanks for any help in advance! Basically, as the title says, I am looking for a way to print the number of times the user gave the program an input, and then also print the average of...
5
by: p3rk3le | last post by:
So, I'm about to do a sequential search on a table (n contents) of random numbers. I have to print the average between the number of comparisons and the contents of the table (n) and the...
1
by: Ormazd | last post by:
Hello, I was wondering if anyone might be able to help me with a little PERL script? I'm very new and I have been given a task to write a simple Perl script that prints out the file names and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.