Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:58:54+00:00 2026-05-27T03:58:54+00:00

I m still in here. ;) I’ve got this code from a very expert

  • 0

I m still in here. 😉
I’ve got this code from a very expert guy, and I’m shy to ask him this basic questions…anyway this is my question now; this Perl Script prints the median of a column of numbers delimited space, and, I added some stuff to get the size of it, now I’m trying to get the sum of the same column. I did and got not results, did I not take the right column? ./stats.pl 1 columns.txt
#!/usr/bin/perl

use strict;
use warnings;

my $index = shift;
my $filename = shift;
my $columns = [];

open (my $fh, "<", $filename) or die "Unable to open $filename for reading\n";

for my $row (<$fh>) {

my @vals = split/\s+/, $row;
push @{$columns->[$_]}, $vals[$_] for 0 .. $#vals;
}

close $fh;

my @column = sort {$a <=> $b} @{$columns->[$index]};

my $offset = int($#column / 2);
my $length = 2 - @column % 2;

my @medians = splice(@column, $offset, $length);

my $median;
$median += $_ for @medians;
$median /= @medians;

print "MEDIAN = $median\n";
################################################
my @elements = @{$columns->[$index]};
my $size = @elements;
print "SIZE = $size\n";
exit 0;
#################################################
my $sum = @{$columns->[$index]};
for (my $size=0; $size < $sum; $size++)  { 
my $mean = $sum/$size;
};
print "$mean\n";

thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:58:55+00:00Added an answer on May 27, 2026 at 3:58 am

    OK some pointers to get you going :

    You can put all the numbers into an array :

    my @result = split(m/\d+/, $line);
    #average 
    use List::Util qw(sum);
    my $sum = sum(@result);
    

    You can then access individual columns with $result[$index] where index is the number of column you want to access.

    Also note that :

    $total = $line + $total;
    $count = $count + 1;
    

    Can be rewritten as :

    $total += $line;
    $count += 1;
    

    Finally make sure that you are reading the file :

    put a “debugging” print into the while loop :

    print $line, "\n";
    

    This should get you going 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: The code here still has some bugs in it, and it could do
Still learning Objective-C / iPhone SDK here. I think I know why this wasn't
I've read similar questions here but I'm still a little confused. MyCollection extends ArrayList<MyClass>
I am still developing this function, but here is what I am intending it
I've read in Expert Python Programming about this edge case. Check this code: def
I've got a test site here (still in development) and I'm trying to get
When I try to execute an external program from java I use this code
noob here still experimenting with templates. Trying to write a message processing class template
Forgive my ignorance - still learning here. I am using Eclipse Ganymede (Java 1.5)
I have been reading up on Cloud computing on here and still not getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.