set default value for undefined variables

M

Morfys

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.
 
B

Ben Morrow

Quoth Morfys said:
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
 
J

Jürgen Exner

Morfys said:
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
 
T

Ted Zlatanov

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
 
E

Eric Pozharski

Morfys said:
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 said:
and initializing each variable $a = ""; b = ""; is a pain.

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

nntpman68

Morfys said:
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
 
D

Dr.Ruud

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];
 
C

cartercc

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
 
W

Willem

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
 
P

Peter Scott

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top