Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > set default value for undefined variables

Reply
Thread Tools

set default value for undefined variables

 
 
Morfys
Guest
Posts: n/a
 
      08-21-2008
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.

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      08-21-2008

Quoth Morfys <(E-Mail Removed)>:
> 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.
~ http://www.velocityreviews.com/forums/(E-Mail Removed) ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      08-21-2008
Morfys <(E-Mail Removed)> 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
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      08-21-2008
On Thu, 21 Aug 2008 09:56:17 -0700 (PDT) Morfys <(E-Mail Removed)> 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
 
Reply With Quote
 
Eric Pozharski
Guest
Posts: n/a
 
      08-21-2008
Morfys <(E-Mail Removed)> 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
 
Reply With Quote
 
nntpman68
Guest
Posts: n/a
 
      08-21-2008
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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      08-22-2008
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."
 
Reply With Quote
 
cartercc
Guest
Posts: n/a
 
      08-22-2008
On Aug 21, 12:56*pm, Morfys <(E-Mail Removed)> 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
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      08-22-2008
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
 
Reply With Quote
 
Peter Scott
Guest
Posts: n/a
 
      08-22-2008
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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help using Spreadsheet::ParseExcel Module - Can't call method "value"on an undefined value perl Newbie Perl Misc 2 05-06-2009 09:43 AM
different handling of local variables and instance variables when undefined Tammo Tjarks Ruby 2 09-13-2007 06:29 PM
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments