Solved

count and divide

Posted on 2002-03-13
1
215 Views
Last Modified: 2010-03-05
I've got a plain text file with some random numbers seperated by comma's, like this:

5,6,4,7,5,5,6,9,10,

How can I open the plain text file and count up the numbers and then divide them by the number of numbers ?
(sorry for my bad english)

in the example above I would like to get as result:
6,3 (1 digit behind the comma)
this is 5+6+4+7+5+5+6+9+10 / 9
0
Comment
Question by:oostwijk
1 Comment
 
LVL 5

Accepted Solution

by:
Sapa earned 50 total points
ID: 6863250
#!/usr/bin/perl -w
use strict;

open(F, '< file.txt') or die $!;
my @digits = <F> =~ /(\d+)/g;
close F;
my $sum = 0;
$sum += $_ for @digits;
print "Result: ", $sum/@digits;
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now