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.
----------------------------------------------------------- -
#!/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 -
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
you need to remove comma. 4 1805
you need to remove comma.
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
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/
Thank you! I appreciate the help. Code works great now! I just had to modify and add the "g" at the end
So now it looks like - #!/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");
-
$filesize =~ s/,//g;
-
$count++;
-
$totalFileSize+=$filesize;
-
}
-
$average = $totalFileSize/$count;
-
print("\nTotal Files: $count \tAverage file size: $average\n");
-
-
close(ALBERT_LAB);
With the output - 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.15385
Thank you, again Chomp
-Albert Cardenas
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |