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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-19-2006, 04:20 AM
|
#1
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440
Rep:
|
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
|
|
|
10-19-2006, 04:34 AM
|
#2
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Your file has a new-line after the actual payload?
Cheers,
Tink
|
|
|
10-19-2006, 05:03 AM
|
#3
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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";
}
|
|
|
10-19-2006, 08:38 AM
|
#4
|
Member
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424
Rep:
|
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:
"use strict;" will also complain about that.
Secondly, what do you think "$num+=$num" will do?
|
|
|
10-19-2006, 05:23 PM
|
#5
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440
Original Poster
Rep:
|
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.
|
|
|
10-19-2006, 06:56 PM
|
#6
|
Member
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424
Rep:
|
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.
|
|
|
10-19-2006, 09:26 PM
|
#7
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440
Original Poster
Rep:
|
Ahh. Thank you, spirit!
-twantrd
|
|
|
All times are GMT -5. The time now is 02:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|