CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Cumulative sum not happening as desired

 



nehavin
New User

Jan 7, 2014, 1:28 AM

Post #1 of 3 (905 views)
Cumulative sum not happening as desired Can't Post

Hi

Please look at the code below:

foreach my $i (0 .. $#tokens)
{
my $token = $tokens[$i];

$nested += $token->[1] if 'PAREN' eq $token->[0];
print $token->[1], $/ if 'FROM' eq uc $tokens[$i - 1][1] && 'TEXT' eq $tokens[$i][0];
print $token->[1], $/ if 'JOIN' eq uc $tokens[$i - 1][1] && 'TEXT' eq $tokens[$i][0];
print $token->[1], $/ if $nested = 0 && 'COMMA' eq $tokens[$i - 1][0];
}

Here the "tokens" array is something like this:

PAREN 1

PAREN 1

PAREN -1

and so on.....

Now I need the variable "nested" to have cumulative sum of the 2nd column of the array.

I get this result if I comment the print statements in the block, if i uncomment then the cumulative some doesn't work.

I am a perl newbie and may be missing something trivial. Please help.


BillKSmith
Veteran

Jan 7, 2014, 4:53 AM

Post #2 of 3 (898 views)
Re: [nehavin] Cumulative sum not happening as desired [In reply to] Can't Post

Always use

Code
use strict; 
use warnings;


In this case, they will tell you explicitly what your error is.
Good Luck,
Bill


BillKSmith
Veteran

Jan 7, 2014, 7:57 AM

Post #3 of 3 (896 views)
Re: [nehavin] Cumulative sum not happening as desired [In reply to] Can't Post

Please indicate that you have cross posted. I do not like finding that that I have wasted my time answering a question that has already been answered several hours earlier on another forumMad
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives