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

Fellow Monks,
I am in need of some assistance. I have a script that has a conditional loop that looks like this:
while (<IN>) { $url = $_; $response = 0; unless($response == 200) { yada yada yada yada.. } }
i need to add another condition to this...i need it to skip to the next entry in the inFile if($response == 500). I tried adding another if statement inside the block that looked like this:
if($response == 500) { goto next; } I also tried just next; and that didn't work.
Does anyone know of a way that i can have it skip the current value of $_ and have it move on to the next line in the inFile. Any help would be appreciated.
Thnx
Ray

Replies are listed 'Best First'.
Re: using unless($code == 200)....
by chromatic (Archbishop) on Sep 12, 2001 at 21:40 UTC
    I doubt that's your problem, but here is what I'd use, with a diagnostic:
    next if $response == 500; warn "->$response<-\n";
    For what it's worth, tye had the best suggestion in that thread. You might also change your loop condition slightly:
    while (defined( my $url = <IN> )) { chomp $url;
Re: using unless($code == 200)....
by dragonchild (Archbishop) on Sep 12, 2001 at 21:37 UTC
    LINE: while (<IN>) { my $url = $_; my $response = 0; next LINE if $response == 500; unless ($response == 200) { # Do stuff here. } }
    Of course, the check for equality to 500 will never work because you set response to 0 and never change it. This means that your unless-clause will always enter the block.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: using unless($code == 200)....
by demerphq (Chancellor) on Sep 12, 2001 at 21:44 UTC
    I also tried just next; and that didn't work.

    So no matter how much you yell at it it wont get a job?

    :-)
    Ok seriously, you'll have to explain what you mean by that cause next is the way to do this. Of course you havent said where $response gets set so its a bit hard to tell where the next should go of course, so I've done it like this:

    while (<IN>) { $url = $_; $response = somefunc(); next if $response==500; unless($response == 200) { } }
    HTH

    Yves
    --
    You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Re: using unless($code == 200)....
by acid06 (Friar) on Sep 12, 2001 at 21:50 UTC
    Hmmm... well... I just tested and next if $response == 500 works as expected:
    use strict; my @responses = (500, 300, 200, 400, 500, 300, 200, 500, 400, 500); while (<DATA>) { chomp; my $response = $responses[$_]; unless ($response == 200) { next if $response == 500; print "[$_] $response\n"; } } __DATA__ 0 1 2 3 4 5 6 7 8 9
    outputs, as expected:
    [1] 300 [3] 400 [5] 300 [8] 400
    correctly skipping the responses which are either 200 or 500... so your problem might actually be another part of the code...

    acid06
    perl -e "print pack('h*', 16369646), scalar reverse $="
Re: using unless($code == 200)....
by seesik (Initiate) on Sep 12, 2001 at 21:44 UTC
    hola.
    first off, it usually helps if we can see a more complete chunk of your code. in your pasted example, $response will always be 0. i know that people have a tendency to paste 'demo' code, but the more representative that your example is, the more accurate our response can be.
    so let's presume that you're manipulating $response somehow. while perl doesn't have a built-in switch statement, you can always use a simple if-elsif-else structure. e.g.
    if ($response == 200) { # } elsif ($response == 500) { next; # or last, or whatever. } else { # default }

    or, if you just want to skip to the next line if a specific condition is met, try
    next if ($response == 500);
    just as in english, it's generally more clear to ask in the affirmative: "is it raining?" vs. "it is not raining, is it"? that's often applicable for your code as well with if vs. unless.

    also fyi, goto needs a label (or expression that returns a label), not another keyword. if you'd use strict and -w, you'd probably catch these things...
    cheers..

Re: using unless($code == 200)....
by broquaint (Abbot) on Sep 12, 2001 at 21:49 UTC
    If you just want to skip the loop when $response == 500 you could try putting this at the beginning of your loop -
    next if 500 == $response;
    this should go to the next iteration of the loop if $response is equal to 500. If you're still having troubles, you'll probably want to check that $response actually reaches 500 as it may be getting lost somewhere along the line.
    I'd also recommend using the CONSTANT == $VARIABLE coding style as it keeps your code safe from accidental conditional assignments (we don't want $response = 500!).
    HTH

    broquaint