RE: One line variable declaration with multiple conditions

2004-01-07 Thread Bob Showalter
Dan Muey wrote:
> Howdy list.
> I'm trying to one lineify this:
> 
> my $guts = $firstchoice || '';
> if(!$guts && $use_second_choice_if_first_is_empty) {  $guts =
> $secondchoice; }
> 
> Basically
> my $guts = $firstchoice || $secondchoic || '';
> Would be perfect except I only want to let it use
> $seconchoice if $use_second_choice_if_first_is_empty has a true value.
> This does not work like I want but illustrates the goal if
> you read it our loud.
> my $guts = $firstchoice || $secondchoic if
> $use_second_choice_if_first_is_empty || '';
> 
> Is that possible to do with one line?

  $first || ($use_second && $second) || '';

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: One line variable declaration with multiple conditions

2004-01-07 Thread Wiggins d Anconia


> Howdy list.
> I'm trying to one lineify this:
> 
> my $guts = $firstchoice || ''; 
> if(!$guts && $use_second_choice_if_first_is_empty) { 
>   $guts = $secondchoice;
> }
> 
> Basically 
> my $guts = $firstchoice || $secondchoic || ''; 
> Would be perfect except I only want to let it use $seconchoice if
$use_second_choice_if_first_is_empty has a true value.
> This does not work like I want but illustrates the goal if you read it
our loud.
> my $guts = $firstchoice || $secondchoic if
$use_second_choice_if_first_is_empty || '';
> 
> Is that possible to do with one line?
> 
> TIA
> 
> Dan

Seems like the ternary operator should set you up... perldoc perlop look
for "Conditional Operator"...

my $guts = (($firstchoice ne '') ? $firstchoice : (($firstchoice eq '')
? $secondchoice : ''));

Though there may be a better way using some combination of ||= or am I
misunderstanding you completely?

http://danconia.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: One line variable declaration with multiple conditions

2004-01-07 Thread drieux
On Jan 7, 2004, at 2:20 PM, Dan Muey wrote:

Is that possible to do with one line?
technically no, because you needed
my ($firstchoice, $use_second_choice_if_first_is_empty);
my $secondchoice = 's2';
then you can do

my $guts = ($use_second_choice_if_first_is_empty)? $secondchoice : 
$firstchoice || '';

ciao
drieux
---

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: One line variable declaration with multiple conditions

2004-01-07 Thread Dan Muey
> 
> On Jan 7, 2004, at 2:20 PM, Dan Muey wrote:
> 
> >
> > Is that possible to do with one line?
> 
> technically no, because you needed
> my ($firstchoice, $use_second_choice_if_first_is_empty);
> my $secondchoice = 's2';

Sorry, I figured we could assume they were 
declared earlier so as not to muck up the screen and it 
wouldn't really change the question or answer.

> 
> then you can do
> 
> my $guts = ($use_second_choice_if_first_is_empty)? $secondchoice : 
> $firstchoice || '';
> 

Nice.

> 
> ciao
> drieux
> 
> ---
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED] 
 



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: One line variable declaration with multiple conditions

2004-01-07 Thread Dan Muey
> > Howdy list.
> > I'm trying to one lineify this:
> > 
> > my $guts = $firstchoice || '';
> > if(!$guts && $use_second_choice_if_first_is_empty) { 
> > $guts = $secondchoice;
> > }
> > 
> > Basically
> > my $guts = $firstchoice || $secondchoic || ''; 
> > Would be perfect except I only want to let it use $seconchoice if
> $use_second_choice_if_first_is_empty has a true value.
> > This does not work like I want but illustrates the goal if 
> you read it
> our loud.
> > my $guts = $firstchoice || $secondchoic if
> $use_second_choice_if_first_is_empty || '';
> > 
> > Is that possible to do with one line?
> > 
> > TIA
> > 
> > Dan
> 
> Seems like the ternary operator should set you up... perldoc 
> perlop look for "Conditional Operator"...
> 
> my $guts = (($firstchoice ne '') ? $firstchoice : 
> (($firstchoice eq '') ? $secondchoice : ''));

Thanks! I'll try each of these suggestions and maybe Benchmark them for grins.

> 
> Though there may be a better way using some combination of 
> ||= or am I misunderstanding you completely?
> 
http://danconia.org


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: One line variable declaration with multiple conditions

2004-01-07 Thread Dan Muey
> Dan Muey wrote:
> > Howdy list.
> > I'm trying to one lineify this:
> > 
> > my $guts = $firstchoice || '';
> > if(!$guts && $use_second_choice_if_first_is_empty) {$guts =
> > $secondchoice; }
> > 
> > Basically
> > my $guts = $firstchoice || $secondchoic || '';
> > Would be perfect except I only want to let it use $seconchoice if 
> > $use_second_choice_if_first_is_empty has a true value. This 
> does not 
> > work like I want but illustrates the goal if you read it our loud.
> > my $guts = $firstchoice || $secondchoic if
> > $use_second_choice_if_first_is_empty || '';
> > 
> > Is that possible to do with one line?
> 
>   $first || ($use_second && $second) || '';
> 

Short and sweet! Thanks Bob!

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: One line variable declaration with multiple conditions

2004-01-07 Thread Dan Muey
> > Howdy list.
> > I'm trying to one lineify this:
> > 
> > my $guts = $firstchoice || '';
> > if(!$guts && $use_second_choice_if_first_is_empty) { 
> > $guts = $secondchoice;
> > }
> > 
> > Basically
> > my $guts = $firstchoice || $secondchoic || ''; 
> > Would be perfect except I only want to let it use $seconchoice if
> $use_second_choice_if_first_is_empty has a true value.
> > This does not work like I want but illustrates the goal if 
> you read it
> our loud.
> > my $guts = $firstchoice || $secondchoic if
> $use_second_choice_if_first_is_empty || '';
> > 
> > Is that possible to do with one line?
> > 
> > TIA
> > 
> > Dan
> 
> Seems like the ternary operator should set you up... perldoc 
> perlop look for "Conditional Operator"...
> 
> my $guts = (($firstchoice ne '') ? $firstchoice : 
> (($firstchoice eq '') ? $secondchoice : ''));

Except I only want to assign $secondchoice to it if  
$use_second_choice_if_first_is_empty is true.

BTW - I'm not really using these variable names, only using them here ot help clarify 
my goal.
Bob's woprks perfect and I havn';t tied Driex's as I have to leave right now. 
I'm going to benchmark them and see if there is any performance gain in either, though 
I doubt it.

Thanks
> 
> Though there may be a better way using some combination of 
> ||= or am I misunderstanding you completely?
> 
http://danconia.org


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: One line variable declaration with multiple conditions

2004-01-07 Thread R. Joseph Newton
Dan Muey wrote:

> Howdy list.
> I'm trying to one lineify this:
>
> my $guts = $firstchoice || '';
> if(!$guts && $use_second_choice_if_first_is_empty) {
> $guts = $secondchoice;
> }
>
> Basically
> my $guts = $firstchoice || $secondchoic || '';
> Would be perfect except I only want to let it use $seconchoice if 
> $use_second_choice_if_first_is_empty has a true value.
> This does not work like I want but illustrates the goal if you read it our loud.
> my $guts = $firstchoice || $secondchoic if $use_second_choice_if_first_is_empty || 
> '';
>
> Is that possible to do with one line?
>
> TIA
>
> Dan

I don't know Dan, it looks like you already said it clearly in the first cnstruct 
above.  I would not discard that clarity
without good reason.  Isn't it more important to say what you mean than to tuck it all 
up tightly?

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: One line variable declaration with multiple conditions

2004-01-07 Thread R. Joseph Newton
Dan Muey wrote:

> BTW - I'm not really using these variable names, only using them here ot help 
> clarify my goal.

Why not?  They looked very good to me, at least in the context of the question at 
hand.  One of the
best aspects of Perl, IMHO, is that it allows you to just say what you mean.  I think 
people take far
too little advantage of this boon.

>
> Bob's woprks perfect

I agree.  It is quite elegant, with no loss of clarity.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: One line variable declaration with multiple conditions

2004-01-08 Thread Dan Muey
> Dan Muey wrote:
> 
> > Howdy list.
> > I'm trying to one lineify this:
> >
> > my $guts = $firstchoice || '';
> > if(!$guts && $use_second_choice_if_first_is_empty) {
> > $guts = $secondchoice;
> > }
> >
> > Basically
> > my $guts = $firstchoice || $secondchoic || '';
> > Would be perfect except I only want to let it use $seconchoice if 
> > $use_second_choice_if_first_is_empty has a true value. This 
> does not 
> > work like I want but illustrates the goal if you read it 
> our loud. my 
> > $guts = $firstchoice || $secondchoic if 
> > $use_second_choice_if_first_is_empty || '';
> >
> > Is that possible to do with one line?
> >
> > TIA
> >
> > Dan
> 
> I don't know Dan, it looks like you already said it clearly 
> in the first cnstruct above.  I would not discard that 

It is clear but doesn't do what I'm getting at.
The seconde one *reads* like it but does not function how it *reads*.
I want to assign it a value.
If that value is missing I want to assign a second value, and 
here's the tricky part, if a particular variable says it is ok to.
Othyerwise it should be empty.

> clarity without good reason.  Isn't it more important to say 
> what you mean than to tuck it all up tightly?

Except I need to do this to about ten variabels all in a row. 
Which gives me 10 lines with Bob's way and 40 with my very 
first example. Boo for that! :)

> 
> Joseph
> 
> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: One line variable declaration with multiple conditions

2004-01-08 Thread Dan Muey

> Dan Muey wrote:
> 
> > BTW - I'm not really using these variable names, only using 
> them here 
> > ot help clarify my goal.
> 
> Why not?  They looked very good to me, at least in the 
> context of the question at hand.  One of the best aspects of 

In context yes, but the really really long was a bit much!

> Perl, IMHO, is that it allows you to just say what you mean.  
> I think people take far too little advantage of this boon.
> 

It definitely does help, especially after you look at the code 
again a while later and you're trying to remember what you were doing, or
Someone else is tryign to decifer it later.

Thanks for bringing that point out.

> >
> > Bob's woprks perfect
> 
> I agree.  It is quite elegant, with no loss of clarity.
> 
> Joseph
> 
> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: One line variable declaration with multiple conditions

2004-01-08 Thread drieux
On Jan 8, 2004, at 7:45 AM, Dan Muey wrote:
[..]
Except I need to do this to about ten variabels all in a row.
Which gives me 10 lines with Bob's way and 40 with my very
first example. Boo for that! :)
[..]

Have you thought about a simplification process?
One of the tricks I do is say
	my ($var1,$var2,$var3,$var4) = ('' , '' , '', '');

This way I have declared them and initialized them,
so that they will not bounce with that 'use of undefined
var at line X" error.
This way when you get down to

	my $guts = ($use_second_choice)? $var2:$var1;

you don't need to worry about the additional bit of
making sure that it is
	my $guts = ($use_second_choice)? $var2: $var1 || '';

Just a thought to think...

ciao
drieux
---

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: One line variable declaration with multiple conditions

2004-01-08 Thread Dan Muey
> On Jan 8, 2004, at 7:45 AM, Dan Muey wrote:
> [..]
> >
> > Except I need to do this to about ten variabels all in a row. Which 
> > gives me 10 lines with Bob's way and 40 with my very first example. 
> > Boo for that! :)
> >
> [..]
> 
> Have you thought about a simplification process?
> One of the tricks I do is say
> 
>   my ($var1,$var2,$var3,$var4) = ('' , '' , '', '');
> 
> This way I have declared them and initialized them,
> so that they will not bounce with that 'use of undefined
> var at line X" error.
> 
> This way when you get down to
> 
>   my $guts = ($use_second_choice)? $var2:$var1;
> 
> you don't need to worry about the additional bit of
> making sure that it is
> 
>   my $guts = ($use_second_choice)? $var2: $var1 || '';
> 

Wouldn't the || '' apply to $guts and not the use of uninitialized $var1,$var2,etc... 
Anyway?

> Just a thought to think...

The vars to be assigned ($var1, $var2,etc...) come from a 
database query so they are handled already earlier. So how 
they are declared are irrelevant to the issue. (Yes they must be 
initialized for a warnings safe environment and they are, just 
assume that they are so the issue is not clouded by where they come from.)

The other thing  the tricky part is:
I only want to assign it $var2 if($use_second_choice && !$var1)
I think the way you're doing it will asign it $value2 if $use_second_choice 
regardless of if $var1 has a value or not.

So :
No matter what if $var1 has a value then assign $guts that value.
If $var 1 is empty and $use_second_value then assign it $var2
Other wise it shoufl be empty.

Bill's method work perfectly, namely:
$var1,$var2 and $ues_2 are declared and set earlier via a 
DB query. So don't worry about where they are coming from.

my $value = $var1 || ($use_2 && $var2) || '';

So $value gets set to $var1 no matter what if($var1).
If it's not then it goes to the next step and is asking :"Ok $var1 has no 
value do you want to use $var2 ?" if Yes give it $var2 if not say "sorry 
I can't help you" and on the the next || which says "ok you shall receive 
from the variable gods '' nothing!"

OR in longer terms:

my $guts = ''; # so that at least it has something assigned even if it's nothgin. :)
if($var1) { $guts = $var1; }
elsif($use_var2_if_var1_is_empty) { $guts = $var2; }

See what I'm trying to get at?

Bob's example does this quite perfectly, thanks again Bob!\

Dmuey

> 
> ciao
> drieux

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: One line variable declaration with multiple conditions

2004-01-08 Thread drieux
On Jan 8, 2004, at 10:38 AM, Dan Muey wrote:
[..]
The vars to be assigned ($var1, $var2,etc...) come from a
database query so they are handled already earlier. So how
they are declared are irrelevant to the issue. (Yes they must be
initialized for a warnings safe environment and they are, just
assume that they are so the issue is not clouded by where they come 
from.)
I think I better see the context at this point.
note, as I presume you did
my ($var1,$var2,$var3,$var4) = ('', 'bob', '', '');
print "($var1,$var2,$var3,$var4)\n";
 ($var1,$var2,$var3,$var4) = db_query();
print "($var1,$var2,$var3,$var4)\n";
#
#
sub db_query
{
my ($input) = @_;

('',$input);

} # end of db_query
will explode on the second print statement - since the
little db_query() returned only two of four possible variables,
and in this case three of them are undef and thus 'resetting' the 
values.
[..]

hence the problem being that mere 'initialization' alone
is not going to 'save the day'.
Bob's example does this quite perfectly, thanks again Bob!\
yeah, I can see that now.

ciao
drieux
---

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: One line variable declaration with multiple conditions

2004-01-08 Thread Dan Muey
> 
> I think I better see the context at this point.
> note, as I presume you did

The issue is resolved, but for the die hards here goes...
I'm wanted to figure out my method of attack before I did the whole 
thing but here is an example that will illustrate the basic idea hopefully:

#!/usr/bin/perl -w

use strict;
use DBI;

my $dbh->connect(...) or die ;

my ($def_foo,$def_bar,$def_joe,$def_mama) = $dbh->selectrow_array("SELECT 
Foo,Bar,Joe,Mama FROM Default_Settings");

for(@{$dbh->selectall_arrayref("SELECT Foo,Bar,Joe,Mama,Use_Def FROM Whatsits WHERE 
Blah=1234")}) {
my ($foo,$bar,$joe,$mama,$use_def) = @{$_};
my $foo_use = $foo || ($use_def && $def_foo) || '';
my $bar_use = $bar || ($use_def && $def_bar) || '';
my $joe_use = $joe || ($use_def && $def_joe) || '';
my $mama_use = $mama || ($use_def && $def_mama) || '';
print "$foo_use - $bar_use $joe_use $mam_use\n"; # or do whatever with it
}
$dbh->disconnect();

> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]