Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: For loop abortions

by Laurent_R (Canon)
on Mar 17, 2016 at 22:36 UTC ( [id://1158166]=note: print w/replies, xml ) Need Help??


in reply to For loop abortions

I don't see any syntax error in your for loops, but they might be written in a simpler and less error-prone fashion with something like this:
for my $i (0..$#sig) { for my $j ($i+1..$#sig) { print "$sig[$j]\n"; } }
Perhaps it could be made even simpler, but I don't understand well enough what you intend to do to be able to suggest something that might get rid of one of the loops variables.

Update: Another possible way:

for my $i (0..$#sig) { for my $item (@sig[$i+1..$#sig]) { print "$item\n"; } }
second update: fixed an off-by-one error in the first piece of code. Thanks to AnomalousMonk for pointing it out.

third update: fixed yet another an off-by-one error . Thanks to AnomalousMonk for pointing it out.

Replies are listed 'Best First'.
Re^2: For loop abortions
by AnomalousMonk (Archbishop) on Mar 18, 2016 at 12:34 UTC

    But the outer  for my $i (0..scalar @sig) { ... } in both loop examples still seems to me to have an inherent off-by-one error. This error is masked by the fact that  $#sig is used as the range limit in the  $i+1..$#sig range expression of both inner loops.

    But the inherent error (as I consider it) still remains, and must be fixed up or masked or otherwise dealt with in some way, and poses a potential pitfall for future programmers and maintainers. Why not use the more idiomatic (and IMHO correct)  for my $i (0 .. $#sig) { ... } form in both outer loops?


    Give a man a fish:  <%-{-{-{-<

      Yes, you are absolutely right, AnomalousMonk, I copied and pasted part of the original code too hastily. This is still quite messy, even if it works.

      I'll fix that for future reference immediately after my answer.

      The other point is that I am not convinced by the algorithm and I suspect it is probably not really what the OP wants, I have trouble finding a use case that would really make sense out of that, but I do not know what the OP wants would really and we don't have enough information, at least not at this point, to be sure.

      Thank you very much.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found