On Tuesday 06 July 2004 23:49, Bryan Harris wrote:
>
> I found this construct in the Perl Cookbook:
>
> $Current_Screen = param(".State") || "Default";
>
> I thought it was really cool because if the value of param(".State")
> comes back undefined, the $Current_Screen variable gets set to
> "Default".
>
> So the other day I wanted to do something similar:
>
> @somearray = @array1 || @array2;
>
> ... hoping that if @array1 was empty, @somearray would get the
> contents of @array2.  But it doesn't work -- @somearray gets (1).
>
> Is what I'm trying to do possible using a single command?  Or is an
> if-then-else the only way to do it?

As you have noticed the logical or operator has problems with list 
context.  You need to use the conditional operator instead.

$ perl -le'
@a = qw/a b c d/;
@b = qw/e f g h/;
@c = ();
print "[EMAIL PROTECTED] = @a : [EMAIL PROTECTED] = @b : [EMAIL PROTECTED] = @c";
@d = @a || @b;
@e = @b || @c;
@f = @c || @a;
print "[EMAIL PROTECTED] = @d : [EMAIL PROTECTED] = @e : [EMAIL PROTECTED] = @f";
@g = @a ? @a : @b;
@h = @b ? @b : @c;
@i = @c ? @c : @a;
print "[EMAIL PROTECTED] = @g : [EMAIL PROTECTED] = @h : [EMAIL PROTECTED] = @i";
'
@a = a b c d : @b = e f g h : @c = 
@d = 4 : @e = 4 : @f = a b c d
@g = a b c d : @h = e f g h : @i = a b c d



John
-- 
use Perl;
program
fulfillment


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to