Solved

count and divide

Posted on 2002-03-13
1
216 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cpan issue 1 61
Shell script to bind my job to half the number of vCPUs in Solaris 8 115
collecting information 2 140
Perl Write to Specific line in a file 15 76
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

23 Experts available now in Live!

Get 1:1 Help Now