At 09:35 AM 3/7/07 -0800, Tom Phoenix wrote:
>On 3/7/07, Karyn Williams <[EMAIL PROTECTED]> wrote:
>
>> I have a script where I am trying to use the conditional operator.
>> Apparently I am confused as it always evaluates false.
>
>> $initial ?
>> $gecos = "$first $initial $last, $sid, SIR Fall 2007":
>> $gecos = "$first $last, $sid, SIR Fall 2007";
>
>It's a very confusing operator. One thing about it that's confusing is
>its precedence: just higher than assignment. That means that your code
>is really saying something like this:
>
> ($initial ? ($gecos = "fred") : $gecos)
> = "barney";
>
>In other words, you're assigning the second string to $gecos in either
>case (right on top of the first string, if $initial was true). If you
>put in the needed parentheses, I think it would do what you expect,
>even though it seems to be an odd programming style.
>
>Are you using the return value from the conditional operator? If not,
>you should code this as an ordinary if/else. But this isn't an
>unreasonable use of this operator. I think what you really meant was
>more like this, maybe?
>
> $gecos = $initial ?
> "$first $initial $last, $sid, SIR Fall 2007" :
> "$first $last, $sid, SIR Fall 2007";
>
>I didn't have to use parentheses here for the same reason that your
>code needed them: The precedence of ?: binds more tightly to its
>arguments than = binds to its arguments.
Thanks.
Here is my attempt at an if statement. However it seems to do the same
thing. I have printed $initial as you can see in the output below. I am
still having the same problem. I know they are both running the else
because of the double space between the first and last name in the second
example. Is there a better way to check if a string is null ?
print ":$initial:\n";
if ($initial =~ "") {
$gecos = "$first $last, $sid, SIR Fall 2007";
} else {
$gecos = "$first $initial $last, $sid, SIR Fall 2007"; }
print "$login\:\:\:\:\:\:$gecos,\:\:$shell\:\n";
:A:
ltesting::::::Lisa A Testing, 999998, SIR Fall 2007,::/bin/sh/:
::
fltestjr::::::ftest ltest Jr., 999999, SIR Fall 2007,::/bin/sh/:
--
Karyn Williams
Network Services Manager
California Institute of the Arts
[EMAIL PROTECTED]
http://www.calarts.edu/network
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/