set default value for undefined variables

Discussion in 'Perl Misc' started by Morfys, Aug 21, 2008.

  1. Morfys

    Morfys Guest

    Is there a way to set the default value for undefined variables? For
    example, in

    my ($a, $b) = (split(/\s+/, $mytext))[0,1]

    sometimes mytext will contain two (or more) words, and sometimes it
    will contain only 1. In the latter case, I would like $b to be
    initialized to "" not undef.

    I have many statements (>60) of the above form with different
    variables, and initializing
    each variable $a = ""; b = ""; is a pain. Thanks.
    Morfys, Aug 21, 2008
    #1
    1. Advertising

  2. Morfys

    Ben Morrow Guest

    Quoth Morfys <>:
    > Is there a way to set the default value for undefined variables? For
    > example, in


    No, there isn't.

    > my ($a, $b) = (split(/\s+/, $mytext))[0,1]
    >
    > sometimes mytext will contain two (or more) words, and sometimes it
    > will contain only 1. In the latter case, I would like $b to be
    > initialized to "" not undef.


    In many cases you can get the effect you want with

    no warnings 'uninitialized';

    which will cause Perl to stop shouting at you about undefs. It will
    silently convert them into '' in string context and 0 in numeric.

    > I have many statements (>60) of the above form with different
    > variables, and initializing
    > each variable $a = ""; b = ""; is a pain. Thanks.


    If you really have all that, consider writing a sub which provides your
    desired defaults.

    Ben

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
    Ben Morrow, Aug 21, 2008
    #2
    1. Advertising

  3. Morfys <> wrote:
    >Is there a way to set the default value for undefined variables? For
    >example, in
    >
    >my ($a, $b) = (split(/\s+/, $mytext))[0,1]
    >
    >sometimes mytext will contain two (or more) words, and sometimes it
    >will contain only 1. In the latter case, I would like $b to be
    >initialized to "" not undef.


    One way: supply an additional optional element for the array slice, such
    that the slice always has at least 2 elements to work with, even if
    split() returns only one:

    my ($a, $b) = (split(/\s+/, $mytext), '')[0,1]

    >I have many statements (>60) of the above form with different
    >variables, and initializing
    >each variable $a = ""; b = ""; is a pain. Thanks.


    Maybe
    my ($a, $b) = ('', '');

    jue
    Jürgen Exner, Aug 21, 2008
    #3
  4. Morfys

    Ted Zlatanov Guest

    On Thu, 21 Aug 2008 09:56:17 -0700 (PDT) Morfys <> wrote:

    M> Is there a way to set the default value for undefined variables? For
    M> example, in

    M> my ($a, $b) = (split(/\s+/, $mytext))[0,1]

    M> sometimes mytext will contain two (or more) words, and sometimes it
    M> will contain only 1. In the latter case, I would like $b to be
    M> initialized to "" not undef.

    M> I have many statements (>60) of the above form with different
    M> variables, and initializing
    M> each variable $a = ""; b = ""; is a pain. Thanks.

    You can do

    my @data = split(/\s+/, $mytext);

    push @data, "" while scalar @data < 2;

    If you have so many statements, you need to abstract the operation. Use
    a hash or an array to store the results logically. If you show more of
    your repetitions we can suggest better ways to organize your code.

    Ted
    Ted Zlatanov, Aug 21, 2008
    #4
  5. Morfys <> wrote:
    *SKIP*
    > I have many statements (>60) of the above form with different
    > variables,


    I believe that you'll achieve better results if you'd hardly rewrite
    your code (I<Del> key is your friend, really).

    > and initializing each variable $a = ""; b = ""; is a pain.


    I don't see B<my>. Hmm,.. Do you C<use strict>, C<use warnings>?

    --
    Torvalds' goal for Linux is very simple: World Domination
    Eric Pozharski, Aug 21, 2008
    #5
  6. Morfys

    nntpman68 Guest

    Morfys wrote:
    > Is there a way to set the default value for undefined variables? For
    > example, in
    >
    > my ($a, $b) = (split(/\s+/, $mytext))[0,1]
    >
    > sometimes mytext will contain two (or more) words, and sometimes it
    > will contain only 1. In the latter case, I would like $b to be
    > initialized to "" not undef.
    >
    > I have many statements (>60) of the above form with different
    > variables, and initializing
    > each variable $a = ""; b = ""; is a pain. Thanks.
    >


    What about:
    my ($a,$b) = (split(' ',$text),'');

    or for more values


    my ($a,$b,$c,$d,$e) = (split(' ',$text),('')x4);


    bye


    N
    nntpman68, Aug 21, 2008
    #6
  7. Morfys

    Dr.Ruud Guest

    nntpman68 schreef:

    > What about:
    > my ($a,$b) = (split(' ',$text),'');
    >
    > or for more values
    >
    >
    > my ($a,$b,$c,$d,$e) = (split(' ',$text),('')x4);



    my @data = split ' ', $line;
    $_ = "" for @data[ ($#data + 1) .. 29];

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Aug 22, 2008
    #7
  8. Morfys

    cartercc Guest

    On Aug 21, 12:56 pm, Morfys <> wrote:
    > Is there a way to set the default value for undefined variables?  For
    > example, in
    >
    > my ($a, $b) = (split(/\s+/, $mytext))[0,1]
    >
    > sometimes mytext will contain two (or more) words, and sometimes it
    > will contain only 1.  In the latter case, I would like $b to be
    > initialized to "" not undef.


    Set $b to 'default' unless $b matches whatever you want, maybe like
    this:
    $b = 'default' unless $b =~ /\w/;

    CC
    cartercc, Aug 22, 2008
    #8
  9. Morfys

    Willem Guest

    Morfys wrote:
    ) Is there a way to set the default value for undefined variables? For
    ) example, in
    )
    ) my ($a, $b) = (split(/\s+/, $mytext))[0,1]

    What is the [0,1] for ? Perl will throw away the other matches anyway,
    and I think it will even optimize away unneeded matches.

    I think the solution I saw crossthread is the best:

    my ($a, $b) = (split(/\s+/, $mytext), '')


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Aug 22, 2008
    #9
  10. Morfys

    Peter Scott Guest

    On Thu, 21 Aug 2008 09:56:17 -0700, Morfys wrote:
    > Is there a way to set the default value for undefined variables? For
    > example, in
    >
    > my ($a, $b) = (split(/\s+/, $mytext))[0,1]
    >
    > sometimes mytext will contain two (or more) words, and sometimes it
    > will contain only 1. In the latter case, I would like $b to be
    > initialized to "" not undef.
    >
    > I have many statements (>60) of the above form with different
    > variables, and initializing
    > each variable $a = ""; b = ""; is a pain. Thanks.


    Repetition is soul-destroying. In addition to the other suggestions made,
    I would add that I think your code could be better designed. Perl has the
    undefined value for a reason and it is usually a useful one. For you to
    erase the distinction between undef and empty string without just using
    Perl's boolean context to do it for you automatically, strikes me as a
    design in need of improvement. Furthermore, sixty plus statements all
    initializing variables at the same scope from split()s will make anyone's
    eyes glaze over. If you want this code to be a lot easier to work with,
    figure out how to factor out the repetition.

    --
    Peter Scott
    http://www.perlmedic.com/
    http://www.perldebugged.com/
    Peter Scott, Aug 22, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor
    Replies:
    70
    Views:
    1,753
    Dan Pop
    Feb 17, 2004
  2. C Gillespie
    Replies:
    3
    Views:
    405
    Peter Hansen
    Mar 22, 2005
  3. Tammo Tjarks
    Replies:
    2
    Views:
    284
    Tammo Tjarks
    Sep 13, 2007
  4. VK
    Replies:
    45
    Views:
    591
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    370
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page