Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

if conditional in nested for loop

by fraizerangus (Sexton)
on Apr 30, 2009 at 17:58 UTC ( [id://761118]=perlquestion: print w/replies, xml ) Need Help??

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

Hi can anybody tell me why I'm not going into the if conditional in the following loop
my $counter = 1; for ($iseg = 1; $iseg <= $nseg; $iseg++){ for ($ires = $beginning[$iseg]; $ires <= $ending[$iseg]; $ires++) +{ if ($ires == $interdomainatoms[$counter]) { print STDOUT $interdomainatoms[$counter]; $counter++; } } }
many thanks in advance!?

Replies are listed 'Best First'.
Re: if conditional in nested for loop
by Corion (Patriarch) on Apr 30, 2009 at 18:03 UTC

    There are many things that don't need to happen, and you haven't shown them to us:

    for ($iseg = 1; $iseg <= $nseg; $iseg++){

    For example, $nseg might be zero, undef or a negative number or some string.

    for ($ires = $beginning[$iseg]; $ires <= $ending[$iseg]; $ires++) +{

    @ending might be empty or contain values numerically smaller than @beginning.

    if ($ires == $interdomainatoms[$counter]) {

    The same counts for @interdomainatoms.

    You don't consider all these to be important, because you haven't shown them to us. So it must be something else that is not in the code you've shown at all. Maybe STDOUT is closed?

Re: if conditional in nested for loop
by kennethk (Abbot) on Apr 30, 2009 at 18:07 UTC
    With what you've provided, no. See How (Not) To Ask A Question. Specifically, without knowing what is in your arrays (@beginning, @ending, @interdomainatoms) there is no way replicate your code's behavior.

    As a stylistic note which has nothing to do with your present query, using foreach loops in place of C-style for loops will make your code less prone to typographical bugs, e.g.

    my $counter = 1; foreach my $iseg (1 .. $nseg){ foreach my $ires ($beginning[$iseg] .. $ending[$iseg]) { if ($ires == $interdomainatoms[$counter]) { print STDOUT $interdomainatoms[$counter]; $counter++; } } }

    See For Loops for details.

Re: if conditional in nested for loop
by otto (Beadle) on Apr 30, 2009 at 18:30 UTC

    Besides what the previous posters have stated, have you said:

    use strict; use warnings;

    at the top of your perl code?

      Hello Everyone, I've included data in the example now and used strict and warnings, but I'm still not able to enter the if conditional; can anyone eluminate why this might be happening please?! many thanks in advance Dan
      #!C:\Perl\bin use strict; use warnings; my @beginning = (34, 76); my @ending = (72, 85); my @interdomainatoms = (33, 34, 72, 73, 75, 76, 77, 78); my $jcounter = 0; my $nseg = 1; for (my $iseg = 0; $iseg <= $nseg; $iseg++){ for (my $ires = $beginning[$iseg]; $ires <= $ending[$iseg]; $i +res++) { if ($ires == $interdomainatoms[$jcounter]) { print STDOUT $ires; $jcounter++; } } }
        I'm still not able to enter the if conditional

        It's no surprise since the first element in @interdomainatoms, 33, is not inside any ranges you defined.

        Just guessing, as still some code seems missing: add another loop for $jcounter to check all @interdomainatoms. There are still places for optimisation, but first the code must work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://761118]
Approved by Corion
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: (None)
    As of 2024-04-25 00:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found