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 8933507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:33:24+00:00 2026-06-15T09:33:24+00:00

I have a Perl question which should be fairly easy but can’t quite seem

  • 0

I have a Perl question which should be fairly easy but can’t quite seem to get it correct I have a file that contains 2 columns of numbers, what I need to is to take the second column and add the first value in this column to the second, the second to the third the third to the fourth etc. I don’t know how many numbers will be in column 2 so I need to do this until the end of the file, I also want to be able to use these values further on in my program. I will give a dummy example below along with the code I have tried so far.

Example file tab delimited file data.txt

1   29
2   26
3   24
4   28

Example of desired out put

55

50

52

Code as is:

#!/usr/bin/perl -w
# use and library files
use strict;
use warnings;


my $line;
my $Val;
my $sum;
open(FH, "data.txt") or die $!;
while (my $line = <FH>){
my @val = split("\t", $line);
my $Val = $val[1];
my $sum = $Val+$Val;
print "$sum\n";
}
close FH;

The line $sum = $Val+$Val; needs to have some sort of loop so the correct values are being added but not sure how to get it right, and then I need to somehow assign the summed values variable names to use later.

Any help would be greatly appreciated

Thanks in advance
Sinead

  • 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-06-15T09:33:25+00:00Added an answer on June 15, 2026 at 9:33 am

    You got it almost in your example. $Val is your previous value. So you must not overwrite it before you used it in the $sum. This means swap these two lines and add the previous and the current value.

    my $sum = $Val+$val[1];
    my $Val = $val[1];
    

    One advice, try to give distinct names to variables. Differences in case only easily lead to confusion.

    Here’s my version

    my $prev = 0;
    while (<>) {
        my(@cols) = split;
        my $sum = $prev + $cols[1];
        $prev = $cols[1];
        print "$sum\n" if ($. > 1); # skip first line
    }
    

    and call it with

    perl sum.pl data.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Perl script for the following bioinformatics question, but unfortunately there
I have Perl code that uses the system() function to call robocopy.exe, but it
I have a perl script URL which gives me a ZIP file, it processes
I have a Perl script that uses WWW::Mechanize to read from a file and
I have a rather obscure question, which I'll probably get shot down for asking,
Apologies for a simple question, but I'm very new to Perl! I have an
I have a HTML file, which generates a web page. Users can enter some
I am currently learning Perl. I have Perl hash that contains references to hashes
I have a Perl subroutine which returns an array of vaules, and I'd like
I have a Perl script that takes user input and creates another script that

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.