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

Hi guys,
I need some guidance on a code i wrote. I am trying to implement a sliding-window technique, in which I have to increment one "for" loop(dependent on the index of larger loop) within another "for" loop.
Now the code i have is
for ($i=$analysisstartposn-1 ; $i<($seqlength-$windowsize) ; $i+$steps +ize) { for ($j = $i ; $j < $i+$windowsize ; $j++) { print OUTFILE "$i\t$j\n"; } }
I use "strict" and "diagnostics" in my script and i get an error that says "useless use of addition (+) in void context at line #(where the for loop with $j is present in the above code)"
If i have a step-size of 2 and a window size of 4, the output i need has to be in the form of:
i j
0 0
0 1
0 2
0 3
2 2
2 3
2 4
2 5
......... and so on

but the output i get, from the above code is in a way that $i does not increase, only $j resets and then increases at the end of its loop. I mean its like this
i j
0 0
0 1
0 2
0 3
0 0
0 1
0 2
0 3
..... and so on

Any help would be appreciated
Thanks in advance

Krish

Replies are listed 'Best First'.
Re: Incrementing one "for" loop within another "for" loop
by davido (Cardinal) on Feb 02, 2011 at 19:14 UTC

    Use the plus equals operator. $i+=$steps


    Dave

Re: Incrementing one "for" loop within another "for" loop
by wind (Priest) on Feb 02, 2011 at 19:16 UTC
    Just missing an equal sign in your incrementor
    for ($i=$analysisstartposn-1 ; $i<($seqlength-$windowsize) ; $i+=$step +size)
    - Miller
      It worked!!! Thanks a lot :)
      I am searching for the nearest wall to bang my head on :P :D
      Have a great day!!
      Krish
Re: Incrementing one "for" loop within another "for" loop
by ikegami (Patriarch) on Feb 02, 2011 at 21:55 UTC
    Your inner loop might be clearer as
    for my $window_offset (0 .. $window_size-1) { my $j = $i + $window_offset; print OUTFILE "$i\t$j\n"; }