nehavin has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Cumulative sum not working as it should be
by BrowserUk (Patriarch) on Jan 07, 2014 at 10:02 UTC
    I get this result if I comment the print statements in the block, if i uncomment then the cumulative some doesn't work.

    Because this: print $token->[1], $/ if $nested = 0 && ... assigns 0 to it.

    That should be: print $token->[1], $/ if $nested == 0 && ...


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thanks for your help. One more doubt please.......what does "$/" do before if?? I tried googling it out but could not get anything.

        see perlvar

               $/      The input record separator, newline by default.

        some ppl are too lazy to type "\n" :)

        Cheers Rolf

        ( addicted to the Perl Programming Language)