"next unless $vrbl" fails

Discussion in 'Perl Misc' started by SandyTipper, Jun 26, 2009.

  1. SandyTipper

    SandyTipper Guest

    I've been stuck on this bit of illogic for days. It is stopping me dead in
    my tracks.
    I have the following code (irrelevant lines omitted and replaced by ...):
    --------------
    COH_LOCK: {
    ...
    COH_LOOP:
    for my $host_key (keys %$hosts_data_ref) {
    ...
    my $host_ok = 1;
    if ($room_number) {
    $host_ok = 0 unless exists
    $dynaroom_data_ref->{$room_number};
    }
    if ($bunch_number) {
    $host_ok = 0 unless exists
    $bunches_data_ref->{$bunch_number};
    }
    next COH_LOOP unless $host_ok;
    clean_up_host($host_key);
    $changes++;
    }#COH_LOOP
    Engine::File->save_data(+HOSTS) if $changes;
    }#COH_LOCK
    ----------------
    Using the debugger, I broke before the "next" statement
    $room_number and $bunch_number are both integers > 1
    both hash elements exist
    as expected, $host_ok is 1
    BUT WHEN STEPPING ON, "clean_up_host" EXECUTES! WHY??
     
    SandyTipper, Jun 26, 2009
    #1
    1. Advertising

  2. SandyTipper

    Jim Gibson Guest

    In article <7F71m.32341$Db2.16669@edtnps83>, SandyTipper
    <> wrote:

    > I've been stuck on this bit of illogic for days. It is stopping me dead in
    > my tracks.
    > I have the following code (irrelevant lines omitted and replaced by ...):
    > --------------
    > COH_LOCK: {
    > ...
    > COH_LOOP:
    > for my $host_key (keys %$hosts_data_ref) {
    > ...
    > my $host_ok = 1;
    > if ($room_number) {
    > $host_ok = 0 unless exists
    > $dynaroom_data_ref->{$room_number};
    > }
    > if ($bunch_number) {
    > $host_ok = 0 unless exists
    > $bunches_data_ref->{$bunch_number};
    > }
    > next COH_LOOP unless $host_ok;
    > clean_up_host($host_key);
    > $changes++;
    > }#COH_LOOP
    > Engine::File->save_data(+HOSTS) if $changes;
    > }#COH_LOCK
    > ----------------
    > Using the debugger, I broke before the "next" statement
    > $room_number and $bunch_number are both integers > 1
    > both hash elements exist
    > as expected, $host_ok is 1
    > BUT WHEN STEPPING ON, "clean_up_host" EXECUTES! WHY??


    'unless' is equivalent to 'if not', so the statement

    next COH_LOOP unless $host_ok;

    will execute the 'next' if $host_ok is 'not true'. Since $host_ok is 1
    (true), the next is not executed, and the program executes the
    clean_up_host in the next line. Perhaps you want:

    next COH_LOOP if $host_ok;

    HTH.

    --
    Jim Gibson
     
    Jim Gibson, Jun 26, 2009
    #2
    1. Advertising

  3. SandyTipper

    SandyTipper Guest

    "Jim Gibson" <> wrote in message
    news:260620091048288080%...
    > In article <7F71m.32341$Db2.16669@edtnps83>, SandyTipper
    > <> wrote:
    >
    >> I've been stuck on this bit of illogic for days. It is stopping me dead
    >> in
    >> my tracks.
    >> I have the following code (irrelevant lines omitted and replaced by ...):
    >> --------------
    >> COH_LOCK: {
    >> ...
    >> COH_LOOP:
    >> for my $host_key (keys %$hosts_data_ref) {
    >> ...
    >> my $host_ok = 1;
    >> if ($room_number) {
    >> $host_ok = 0 unless exists
    >> $dynaroom_data_ref->{$room_number};
    >> }
    >> if ($bunch_number) {
    >> $host_ok = 0 unless exists
    >> $bunches_data_ref->{$bunch_number};
    >> }
    >> next COH_LOOP unless $host_ok;
    >> clean_up_host($host_key);
    >> $changes++;
    >> }#COH_LOOP
    >> Engine::File->save_data(+HOSTS) if $changes;
    >> }#COH_LOCK
    >> ----------------
    >> Using the debugger, I broke before the "next" statement
    >> $room_number and $bunch_number are both integers > 1
    >> both hash elements exist
    >> as expected, $host_ok is 1
    >> BUT WHEN STEPPING ON, "clean_up_host" EXECUTES! WHY??

    >
    > 'unless' is equivalent to 'if not', so the statement
    >
    > next COH_LOOP unless $host_ok;
    >
    > will execute the 'next' if $host_ok is 'not true'. Since $host_ok is 1
    > (true), the next is not executed, and the program executes the
    > clean_up_host in the next line. Perhaps you want:
    >
    > next COH_LOOP if $host_ok;
    >
    > HTH.
    >
    > --
    > Jim Gibson


    DOH! Ever wish you could unsend?
    I got twisted up in all the negative logic and too little sleep.
    Thanks for answering such a dumb question.

    Sandy
     
    SandyTipper, Jun 26, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brett Roy

    Tab Fails on TextBox using COM Interop

    Brett Roy, Sep 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    430
    Brett Roy
    Sep 29, 2003
  2. Sabin
    Replies:
    0
    Views:
    611
    Sabin
    Jun 4, 2004
  3. John Saunders
    Replies:
    1
    Views:
    681
    John Saunders
    Nov 18, 2003
  4. Replies:
    2
    Views:
    299
    Grant Wagner
    Dec 23, 2004
  5. dayo
    Replies:
    11
    Views:
    347
    Ilya Zakharevich
    Dec 16, 2005
Loading...

Share This Page