LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2006, 04:20 AM   #1
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Perl add numbers in 2nd field


I think I'm close to my answer but I can't yet seem to find it. Anyhow, I have a file:
Code:
john 10
mary 20
jack 30
And I'm trying to add up the 2nd column. So, the answer should be 60. Below is my perl code:

Code:
#!/usr/bin/perl

open(FILE, "/tmp/file") or die "Argh!";
while (my $rec=<FILE>){
        chomp($rec);
        my($name,$num)=split(/ / , $rec);
        $num+=$num;
        }
print "$num";
I've tried so many ways but now I'm exhausted. Can anyone see what I'm doing wrong? If I put a print "$num"; inside the while loop, it's adding the same number twice. Hmph. Thanks again!

-twantrd
 
Old 10-19-2006, 04:34 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Your file has a new-line after the actual payload?


Cheers,
Tink
 
Old 10-19-2006, 05:03 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 239Reputation: 239Reputation: 239
one liner:

Code:
billy> perl -pae '$t += $F[1];print "$t:" ' data_file
10:john 10
30:mary 20
60:jack 30

Or here's my version which I made for my use
Code:
#!/usr/bin/perl -ws

sub usage()
{
    print <<"EOF";

# tots up all numbers in a tabular file
# assumes first column is the number to
# be totalled unless specified with -col=n
#
# e.g. try: 'du -s * | total'
#
# or
#
# ls -l | total -col=5
EOF
    die "\n";
}

$h ||= undef;
usage if $h;

$col ||=1;
$col--;

while (<>) {

    chomp;

    next unless /./; # skip blanks
    $number = (split)[$col];
    $total += $number;
    print;
    print " ($number)\n";

}
 
Old 10-19-2006, 08:38 AM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Quote:
Originally Posted by twantrd
Code:
        my($name,$num)=split(/ / , $rec);
        $num+=$num;
There are two reasons why this won't work. As for the first, replacing the first line of your script with the following will give you a hint:
Code:
#!/usr/bin/perl -w
"use strict;" will also complain about that.

Secondly, what do you think "$num+=$num" will do?
 
Old 10-19-2006, 05:23 PM   #5
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Ahh, I see my problem.
Quote:
Secondly, what do you think "$num+=$num" will do?
It will display the number on each iteration by 2x. What I needed was to add another variable that held the number - $total+=$num;. That solved my problem.

However, I have one more question . My file is:
Code:
john 10
mary 20
rick - 
jack 30
My perl code is:
Code:
#!/usr/bin/perl -w

use strict;
use warnings;

my $total=0;
my $num=0;
open(FILE, "/tmp/file") or die "BlAH!";
while (my $rec=<FILE>){
        chomp($rec);
        my($name,$num)=split(/ / , $rec);
                if ($num =~ /^\d+/){
                        $total+=$num;
                }
        }
print "$total\n";
The code prints out the correct sum. However, it gives me this warning:
Quote:
Use of uninitialized value in pattern match (m//) at ./test line 12, <FILE> line 6.
What gives? I already declared variable $num in the beginning. Thanks for guiding me in the right direction guys.

-twantrd

Last edited by twantrd; 10-19-2006 at 05:25 PM.
 
Old 10-19-2006, 06:56 PM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Seems like your data file contains an empty line. The error won't appear otherwise, you could also use the following:
Code:
[...] if ( defined( $num ) && $num =~ /^\d+/){ [...]
Note that the initialization "my $num = 0" won't be visible from within the while loop because of "my ( $name, $num)". This will create a new local variable named $num.
 
Old 10-19-2006, 09:26 PM   #7
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Ahh. Thank you, spirit!

-twantrd
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic Perl - removing white from split field. ryedunn Programming 7 09-13-2006 01:24 AM
perl input field separator Tinkster Programming 5 10-18-2004 05:08 PM
How to extract the 16 byte source name field (Name to add) from the NetBIOS fram (NBF Bassam Linux - Networking 0 05-23-2004 05:58 AM
Help.. how do I add two numbers? Tengil Linux - Newbie 3 03-04-2004 01:58 PM
need perl help calculating fibonacci numbers WorldBuilder Programming 5 12-17-2003 02:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration