Perl sum really inefficient!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl sum really inefficient!!
# 8  
Old 05-06-2009
Code:
open $fh,"<","yourfile";
while(<$fh>){
	my (@t1,@t2,$s1,$s2);
	my @tmp=split(",",$_);
	my @temp=@tmp[2..$#tmp];
	while($#temp >= 0){
		push @t1,(pop @temp);
		push @t2,(pop @temp);
	}
	map {$s1+=$_} @t1;
	map {$s2+=$_} @t2;
	print $tmp[0],",",$tmp[1],",",$s1,",",$s2,"\n";
}

# 9  
Old 05-06-2009
Quote:
@kevin, your sum for fail seems different with awk result of franklin. pls confirm.
My code looks like it is working properly. Assumes there are 48 number fields to sum (24 odd and 24 even). But maybe I am missing something.
Code:
use strict;
use warnings;
my ($suc,$fail) = (0,0);
my $CC = 'whatever';
while (my $line = <DATA>){
   chomp($line);
   my @t = split(/,/,$line);
   next if ($t[0] eq "ID" || $t[0] eq "END");
   for (my $i = 2; $i < $#t; $i+=2){
      $suc += $t[$i];
      print "<$suc> += $t[$i]\n";
   }
   for (my $j = 3; $j <= $#t; $j+=2){
      $fail += $t[$j];
      print "<$fail> += $t[$j]\n";
   }
   print "\n\n$CC,$t[1],$suc,$fail";
}
__DATA__
2,Andrew,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,19,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47


Last edited by KevinADC; 05-06-2009 at 05:34 PM..
# 10  
Old 05-06-2009
Quote:
Originally Posted by summer_cherry
Code:
open $fh,"<","yourfile";
while(<$fh>){
	my (@t1,@t2,$s1,$s2);
	my @tmp=split(",",$_);
	my @temp=@tmp[2..$#tmp];
	while($#temp >= 0){
		push @t1,(pop @temp);
		push @t2,(pop @temp);
	}
	map {$s1+=$_} @t1;
	map {$s2+=$_} @t2;
	print $tmp[0],",",$tmp[1],",",$s1,",",$s2,"\n";
}

The OP was looking for more efficient code, not less efficient.
# 11  
Old 05-12-2009
Thanks Pludi that's sorted it out nicely Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Incredibly inefficient cat | grep script

Hi there, I have 2 files that I am trying to work on. File 1 contains a reference list of unique subscriber numbers ( 7 million entries in total) File 2 contains a list of the subscriber numbers and their tariff (15 million entries in total). This file is in the production system and... (12 Replies)
Discussion started by: Cludgie
12 Replies

3. UNIX for Dummies Questions & Answers

Getting the sum

I am trying to get the sum of the first column of a file. When I use the same method for other files it works just fine... for some reason for the file below it gives me an error that I don't understand... I tried looking at different lines of the file and tried different things, but I still... (7 Replies)
Discussion started by: cosmologist
7 Replies

4. Solaris

How to Sum

Hi I need to incorporate a 'sum' as follows into a script and not sure how. I have a variable per line and I need them to be summed, e.g below 1 23 1,456 1 1 34 46 How do I calculate the sum of all these numbers to ouptut the answer ( 1,562) Thanks in advance (3 Replies)
Discussion started by: rob171171
3 Replies

5. Shell Programming and Scripting

PERL : Group & Sum in hash

Hi, I have a hash which is to be populated by reading data lines from a flat file. I am supposed to read fields 1-5 from the file and load them on to the hash such that fields 1-4 are going to be the hash key-set and field 5 is the hash value. Field 5 is a monetary amount and is supposed... (1 Reply)
Discussion started by: sinpeak
1 Replies

6. Shell Programming and Scripting

perl sum 2nd field in an array

Hi Everyone, ($total+=$_) for @record; assume @record=(1,2,3), so the result is 6. if @record=("1 3","2 3","3 3"), would like to sum up the 2nd field of this array, the result is 9. i tried " ($total+=$) for @record ", cannot, please advice. Thanks ---------- Post updated at 03:45... (1 Reply)
Discussion started by: jimmy_y
1 Replies

7. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies

8. Shell Programming and Scripting

Perl script to find particular field and sum it

Hi, I have a file with format a b c d e 1 1 2 2 2 1 2 2 2 3 1 1 1 1 2 1 1 1 1 4 1 1 1 1 6 in column e i want to find all similar fields ( with perl script )and sum it how many are there for instance in format above. 2 - 2 times 4 - 1 time 6 - 1 time what i use is ... (14 Replies)
Discussion started by: Learnerabc
14 Replies

9. Shell Programming and Scripting

Sum value from selected lines script (awk,perl)

Hello. I face this (2 side) problem. Some lines with this structure. ........... 12345678 4 12345989 13 12346356 205 12346644 74 12346819 22 ......... The first field (timestamp) is growing (or at least equal). 1)Sum the second fields if the first_field/500 are... (8 Replies)
Discussion started by: paolfili
8 Replies

10. Shell Programming and Scripting

sum

Hello everyone I need to write a script that sums numbers passed to it as arguments on the command line and displays the results. I must use a for loop and then rewrite it using a while loop. It would have to output something like 10+20+30=60 this is what I have so far fafountain@hfc:~$ vi sum... (1 Reply)
Discussion started by: Blinky85
1 Replies
Login or Register to Ask a Question