Solved

count and divide

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SIMPLE Perl Regex 1 157
Filtering a file to table 9 101
Linux rename files by reordering dates in the file name 6 34
Any syntax error for this clone.plscript 6 137
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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

806 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