UBB.Dev
Posted By: Idle Logical operators and if-elsif statements - 04/05/2002 1:14 AM
I'm a little bit lost here... eh, I'm trying to write a rather simple statement using if-elsif and those freakin' logical operators confuse me.

This is what I've come up with so far (but doesn't work as expected):

code:
		# get status line
$status_sep = "
";
$append_overwrite = $vars_idle{append_status};

if ($user_profile[31] ne "" && $append_overwrite eq "APPEND"
Posted By: Dave_L Re: Logical operators and if-elsif statements - 04/05/2002 1:38 AM
Could you post a bit more of your code, and explain what "doesn't work as expected" means?
Posted By: Idle Re: Logical operators and if-elsif statements - 04/05/2002 2:49 AM
Sorry, but UBB didn't like my code and everything is screwed (note that I'm shown as "Unregistered").

I contacted Allen and asked him to delete those two topics I posted,
I'll start a new one with a link to my code so UBB doesn't screw up again. :rolleyes:

Edit: Wow, I can reply now, and edit!
Weird...

Anyway, the code can be viewed from here .

Thanks!
Posted By: Dave_L Re: Logical operators and if-elsif statements - 04/05/2002 6:26 AM
I think this does what you want:

code:
# get status line

my $status_sep = '
';
my $status = $user_profile[8];
my $custom_status = $user_profile[31];
my $custom_title = &CustomTitle($status);

if (($status eq 'Moderator'
Posted By: Dave_L Re: Logical operators and if-elsif statements - 04/05/2002 6:32 AM
(This board is messed up, so I'll try again.)

I think this does what you want:

# get status line

my $status_sep = '
';
my $status = $user_profile[8];
my $custom_status = $user_profile[31];
my $custom_title = &CustomTitle($status);

if (($status eq 'Moderator' || $status eq 'Administrator') && $vars_idle{append_status} eq 'APPEND') {
$user_status = $custom_title . ($custom_status ? "$status_sep$custom_status" : '');
}
else { # overwrite
$user_status = $custom_status ? $custom_status : $custom_title;
}
Posted By: Idle Re: Logical operators and if-elsif statements - 04/05/2002 12:09 PM
"Messed up" sounds about right... I have no idea why it doesn't like my code. :rolleyes:

Thanks, I'll give it a try (but if it doesn't work, "I'll be back!")! smile

Edit: Nope, that doesn't seem to do the trick, either.
Posted By: Dave_L Re: Logical operators and if-elsif statements - 04/05/2002 7:08 PM
"doesn't seem to do the trick"

Could elaborate on that? I.e., how do the results differ from what you want?
Posted By: Idle Re: Logical operators and if-elsif statements - 04/05/2002 10:24 PM
When I set it to append mode,
and my admin/mod has a custom status, all it returns is the moderator/admin status.
Posted By: Dave_L Re: Logical operators and if-elsif statements - 04/05/2002 10:54 PM
I've added some debug prints (universal debugging technique #1). Try this, and post at least one START-END section from the debug.txt file which has incorrect results.

my $status_sep = '
';
my $status = $user_profile[8];
my $custom_status = $user_profile[31];
my $custom_title = &CustomTitle($status);

my $debug_file = "$vars_config{VariablesPath}/debug.txt"; # comment-out this line to suppress debug output
if ($debug_file) {
open(my $debug_fh, '>>', $debug_file) or die "$!";
print $debug_fh "START, append-status hack, @{[scalar localtime]}n";
print $debug_fh "status_sep='$status_sep'n";
print $debug_fh "status='$status'n";
print $debug_fh "custom_status='$custom_status'n";
print $debug_fh "custom_title='$custom_title'n";
print $debug_fh "append_status='@{[$vars_idle{append_status}]}'n";
}

if (($status eq 'Moderator' || $status eq 'Administrator') && $vars_idle{append_status} eq 'APPEND') {
$user_status = $custom_title . ($custom_status ? "$status_sep$custom_status" : '');
}
else { # overwrite
$user_status = $custom_status ? $custom_status : $custom_title;
}

if ($debug_file) {
print $debug_fh "user_status='$user_status'n";
print $debug_fh "ENDn";
close($debug_fh) or warn "$!";
}
Posted By: Idle Re: Logical operators and if-elsif statements - 04/05/2002 11:45 PM
It just throws an error at me...
Says it can't use an undefined value as a symbol reference at print $debug_fh "user_status='$user_status'n";
Posted By: Dave_L Re: Logical operators and if-elsif statements - 04/06/2002 12:01 AM
Ok, add my $user_status = ''; at the top (after my $custom_title = ...).
Posted By: Idle Re: Logical operators and if-elsif statements - 04/06/2002 12:06 AM
Same error...
Posted By: Dave_L Re: Logical operators and if-elsif statements - 04/06/2002 12:13 AM
Oops, I declared $debug_fh in the wrong scope. Here's the corrected code:

my $status_sep = '
';
my $status = $user_profile[8];
my $custom_status = $user_profile[31];
my $custom_title = &CustomTitle($status);
my $user_status = '';

my $debug_file = "$vars_config{VariablesPath}/debug.txt"; # comment-out this line to suppress debug output
my $debug_fh;
if ($debug_file) {
open($debug_fh, '>>', $debug_file) or die "$!";
print $debug_fh "START, append-status hack, @{[scalar localtime]}n";
print $debug_fh "status_sep='$status_sep'n";
print $debug_fh "status='$status'n";
print $debug_fh "custom_status='$custom_status'n";
print $debug_fh "custom_title='$custom_title'n";
print $debug_fh "append_status='@{[$vars_idle{append_status}]}'n";
}

if (($status eq 'Moderator' || $status eq 'Administrator') && $vars_idle{append_status} eq 'APPEND') {
$user_status = $custom_title . ($custom_status ? "$status_sep$custom_status" : '');
}
else { # overwrite
$user_status = $custom_status ? $custom_status : $custom_title;
}

if ($debug_file) {
print $debug_fh "user_status='$user_status'n";
print $debug_fh "ENDn";
close($debug_fh) or warn "$!";
}
Posted By: Idle Re: Logical operators and if-elsif statements - 04/06/2002 12:26 AM
Oops, I'm stupid. Forgot to set my custom title... shocked

Here:


START, append-status hack, Fri Apr 5 22:35:32 2002
status_sep='
'
status='Moderator'
custom_status='UBB Haxxor'
custom_title='Cop'
append_status=''
user_status='UBB Haxxor'
END


Seems like it doesn't read from vars_idle anymore or what?
Posted By: Dave_L Re: Logical operators and if-elsif statements - 04/06/2002 1:14 AM
Don't know about %vars_idle. Are you loading it the same way that the standard vars_ hashes are loaded?
Posted By: Idle Re: Logical operators and if-elsif statements - 04/06/2002 4:44 AM
Yap.
The vars_idle are read using my code,
but not when I use your's for some reason...
Posted By: Dave_L Re: Logical operators and if-elsif statements - 04/06/2002 11:32 AM
I added an $append_status variable like you were using originally. If that doesn't work, I'm out of ideas.

my $status_sep = '
';
my $status = $user_profile[8];
my $custom_status = $user_profile[31];
my $custom_title = &CustomTitle($status);
my $append_status = $vars_idle{append_status};
my $user_status = '';

my $debug_file = "$vars_config{VariablesPath}/debug.txt"; # comment-out this line to suppress debug output
my $debug_fh;
if ($debug_file) {
open($debug_fh, '>>', $debug_file) or die "$!";
print $debug_fh "START, append-status hack, @{[scalar localtime]}n";
print $debug_fh "status_sep='$status_sep'n";
print $debug_fh "status='$status'n";
print $debug_fh "custom_status='$custom_status'n";
print $debug_fh "custom_title='$custom_title'n";
print $debug_fh "append_status='$append_status'n";
}

if (($status eq 'Moderator' || $status eq 'Administrator') && $append_status eq 'APPEND') {
$user_status = $custom_title . ($custom_status ? "$status_sep$custom_status" : '');
}
else { # overwrite
$user_status = $custom_status ? $custom_status : $custom_title;
}

if ($debug_file) {
print $debug_fh "user_status='$user_status'n";
print $debug_fh "ENDn";
close($debug_fh) or warn "$!";
}
Posted By: Idle Re: Logical operators and if-elsif statements - 04/06/2002 8:09 PM
Doesn't work either. :rolleyes:

But thanks for your help,
the debug part will be useful if I keep on playing around with my code. smile
Posted By: Idle Re: Logical operators and if-elsif statements - 04/07/2002 10:28 AM
I got it!
Works like a charm now. smile

I appreciate your help, it was a good reminder for me.
Most of the time if-else statements confuse the hell out of me, so I try to go for something complex and ugly looking...
I got it to do what I want in 8 lines of code,
instead of 20 or so. laugh
Posted By: Dave_L Re: Logical operators and if-elsif statements - 04/07/2002 12:06 PM
In the past, I've found decision tables useful when dealing with complex if-else situations. I just did a web search, and came up with this link . There may be better references.

By the way, how did you fix the problem? (just curious)
Posted By: Idle Re: Logical operators and if-elsif statements - 04/07/2002 10:40 PM
I removed the status checking (Moderator/Administrator) after figuring out this would affect normal members in some awful way (no status shown at all, haha).

Here's the finished code:
-------------------------

# get status line
&RequireVars("$vars_config{VariablesPath}/vars_idle.cgi");

# get status separator
if ($vars_idle{status_sep} ne ''){
$status_sep = "$vars_idle{status_sep}";
} else {
$status_sep = "
";
}# end get status separator

$status = $user_profile[8];
$custom_status = $user_profile[31];

if ($vars_idle{append_status} eq 'APPEND' && $custom_status ne '') {
$user_status = &CustomTitle($status) . $status_sep . $user_profile[31];
}
elsif ($vars_idle{append_status} eq 'APPEND' && $custom_status eq '') {
$user_status = &CustomTitle($status);
}
elsif ($vars_idle{append_status} eq 'OVERWRITE' && $custom_status ne '') {
$user_status = $user_profile[31];
}
elsif ($vars_idle{append_status} eq 'OVERWRITE' && $custom_status eq '') {
$user_status = &CustomTitle($status);
}
#end get status line
-------------------------

I don't get it, but sometimes it would read vars_idle.cgi, sometimes not.
So I added the RequireVars and "completed" the if-elsif part so I'd definately get the status looking the way I want it to be.

Interesting site, by the way.
Thanks!
© UBB.Developers