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

  • SEARCH
  • Home
  • 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 6860693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:26:26+00:00 2026-05-27T02:26:26+00:00

I want to write a Perl Script that reads a file.txt with columns of

  • 0

I want to write a Perl Script that reads a file.txt with columns of numbers,

20  30  12
31  20  54
63  30  21
11  12  10

and do some calculations, for example the mean. I don’t know how to declare and initialize it.
I’ve got this example, in which is looking for the median, and it has the data declared, in my case, the data is in a file, not in the script and want to calculate the median.
there is it..
#!/usr/bin/perl

#data points 
@vals = ( 33, 23, 55, 39, 41, 46, 38, 52, 34, 29, 27, 51, 33, 28 ); 
print "UNSORTED: @vals\n"; 

#sort data points 
@vals = sort(@vals); 
print "SORTED: @vals\n"; #test to see if there are an even number of data points 
if( @vals % 2 == 0) { 

#if even then: 
$sum = $vals[(@vals/2)-1] + $vals[(@vals/2)]; 
$med = $sum/2; 
print "The median value is $med\n";
}
else{ 
#if odd then: 
print "The median value is $vals[@vals/2]\n";
} 
exit;
  • 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-27T02:26:27+00:00Added an answer on May 27, 2026 at 2:26 am

    This shell one-liner multiply first col with second one :

    perl -lane 'print $F[0] * $F[1]' <FILE>
    

    EDIT: And the perl script version with your new requirements and the file with 3 cols :

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my (@vals, $sum, $med);
    
    while (<>) {
        @vals = split;
    
        print "UNSORTED: @vals\n"; 
    
        #sort data points 
        @vals = sort(@vals); 
        print "SORTED: @vals\n"; #test to see if there are an even number of data points 
    
        if(@vals % 2 == 0) { 
            #if even then: 
            $sum = $vals[(@vals/2)-1] + $vals[(@vals/2)]; 
            $med = $sum/2; 
            print "The median value is $med\n";
        }
        else{ 
            #if odd then: 
            print "The median value is $vals[@vals/2]\n";
        } 
    
        print "\n";
    }
    

    You may understand what’s going on instead of just cut & paste 😉

    To run the script :

    ./script.pl file_with_cols.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a perl script that changes its working directory to somewhere
I want to write a JAR file that makes use of the javax servlet
I want to write a perl script to auto login a website and do
I'm writing a Chrome extension, and want to write one JS file, that provides
I am working on a Perl script to read CSV file and do some
I am attempting to write a one-line Perl script that will toggle a line
I want to write either a perl (CGI) or php script on a server
I've got a Perl script that I want to invoke from a Python script.
I need to write a Perl script to read in a file, and delete
I want to run a Perl script with some while(1) loop in the background

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.