"next unless $vrbl" fails

S

SandyTipper

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??
 
J

Jim Gibson

SandyTipper said:
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.
 
S

SandyTipper

Jim Gibson said:
'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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top