Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
How to get the mean?

Posted by revtopo (revtopo), 11 December 2007
hi all,

I would like to get the counts that are greater than the mean. but since it is within the loop i couldnot get what i needed. Instead all the counts.
The file would be some thing like this

1            1             200            3
1             50           250            12
1              100        300             16

The files are tab delimited. Counts represents the fourht coulmn.
Is there any other way? i have attached my program along.
#! usr/bin/perl -w
use strict;
use Getopt::Long;

my $file;
my $maximum_gap =150;
my %pro_locus;
        'g=i' => \$maximum_gap) or exit;
warn "\n Can't proceed!!!No file name given\n" if !$file;

open (FILE ,"$file") or die "Cannot open the file\n";
my @hit_clusters = <FILE>;

my $line_count = `wc -l < $file`;
my $total_count = 0;

print " the number of line:$line_count\n";
close FILE;
#parsing tthe file
foreach my $file_line(@hit_clusters){

   next if $file_line =~m/^\s*$/;
   next if $file_line =~m/^(Chromosome.+)$/;
   #print "$file_line\n";
   if ($file_line =~m/^(.+?)\t(\d+?)\t(\d+?)\t(\d+?)\b/){
     my ($chro_id, $hit_start, $hit_stop, $count)= ($1, $2, $3, $4);
           my $mean = $total_count/$line_count;
     #print "the mean is $mean \n";
     if ($count >= $mean){




Posted by admin (Graham Ellis), 11 December 2007
Don't think you need code that's as long as that ... try:

@info = <DATA>;

$sum += (split)[-1] foreach (@info);
$mean =  $sum / @info;
print (grep ((split)[-1] > $mean,@info));

1       1       200     3
1       50      250     12
1       100     300     16

Posted by revtopo (revtopo), 12 December 2007
Could you please explain your code. Since i get only the list of file contents rather than mean?

Posted by admin (Graham Ellis), 12 December 2007
Oh - I misunderstood - I thought you wanted all lines from the file who's 4th value was in excedd of the mean of the 4th values.   If you pint $mean as well, you'll see the mean as well.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho