Solved

count and divide

Posted on 2002-03-13
1
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
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…

749 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