One line variable declaration with multiple conditions

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default One line variable declaration with multiple conditions

    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
    Dan Muey Guest

  2. Similar Questions and Discussions

    1. Multiple Conditions for Server Control
      I have a server control that needs to be rendered upon meeting certain conditions that our HTML people can alter. This works fine if the multiple...
    2. Multiple OR conditions in CFIF
      Is there a more elegant way to write the following CFIF statement? It seems very cumbersome. <cfif URL.filterName EQ "E" OR URL.filterName EQ "S"...
    3. Continuing an Arrary declaration on the next line
      Hi All: I would like to know how to continue the declaration of an array on the next line. I have tried: & _ with no luck. For example, ...
    4. Global Variable declaration
      For some reason this wont compile, it says that i need to declare the variable before using it on prepareMovie global sm204
    5. Abstraction of table variable declaration
      Hi all, I'm trying to simplify the maintenance of my stored procedures using SQL Server 2000's table variables. Currently each sp returns...
  3. #2

    Default RE: One line variable declaration with multiple conditions

    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) || '';
    Bob Showalter Guest

  4. #3

    Default Re: One line variable declaration with multiple conditions


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

    [url]http://danconia.org[/url]

    Wiggins D Anconia Guest

  5. #4

    Default Re: One line variable declaration with multiple conditions


    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

    ---

    Drieux Guest

  6. #5

    Default RE: One line variable declaration with multiple conditions

    >
    > 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]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>


    Dan Muey Guest

  7. #6

    Default RE: One line variable declaration with multiple conditions

    > > 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?
    >
    [url]http://danconia.org[/url]

    Dan Muey Guest

  8. #7

    Default RE: One line variable declaration with multiple conditions

    > 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!
    Dan Muey Guest

  9. #8

    Default RE: One line variable declaration with multiple conditions

    > > 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?
    >
    [url]http://danconia.org[/url]

    Dan Muey Guest

  10. #9

    Default Re: One line variable declaration with multiple conditions

    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

    R. Joseph Newton Guest

  11. #10

    Default Re: One line variable declaration with multiple conditions

    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

    R. Joseph Newton Guest

  12. #11

    Default RE: One line variable declaration with multiple conditions

    > 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
    >
    >
    Dan Muey Guest

  13. #12

    Default RE: One line variable declaration with multiple conditions

    > 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
    >
    >
    Dan Muey Guest

  14. #13

    Default Re: One line variable declaration with multiple conditions


    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

    ---

    Drieux Guest

  15. #14

    Default RE: One line variable declaration with multiple conditions

    > 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
    Dan Muey Guest

  16. #15

    Default Re: One line variable declaration with multiple conditions


    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

    ---

    Drieux Guest

  17. #16

    Default RE: One line variable declaration with multiple conditions

    >
    > 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();
    >
    Dan Muey Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139