saying my $a=0; my $b=0; more compactly

D

Dan Jacobson

How does one write
my $a=0; my $b=0; my $c=0;
as compactly as possible (blanks OK)?
my ($a,$b,$c); $a=$b=$c=0;?
my ($a,$b,$c)=(0,0,0);?
But always I end up saying something over again.

Under use strict, I've got a lot of variables to declare and
initialize to zero, but all I can find are non streamlined ways of
doing it. Perhaps I should consider map()...
 
A

A. Sinan Unur

How does one write
my $a=0; my $b=0; my $c=0;
as compactly as possible (blanks OK)?
my ($a,$b,$c); $a=$b=$c=0;?
my ($a,$b,$c)=(0,0,0);?
But always I end up saying something over again.

Under use strict, I've got a lot of variables to declare and
initialize to zero, but all I can find are non streamlined ways of
doing it. Perhaps I should consider map()...

use strict;
use warnings;

my ($a, $b, $c) = ( 0 ) x 3;

print "$a\t$b\t$c\n";
__END__

C:\Home\asu1> t
0 0 0

Sinan
 
T

Tad McClellan

Dan Jacobson said:
How does one write
my $a=0; my $b=0; my $c=0;
as compactly as possible (blanks OK)?


my $a = my $b = my $c = 0;

Under use strict, I've got a lot of variables to declare and
initialize to zero,


Why do you feel the need to initialize them to zero?
 
J

John W. Krahn

A. Sinan Unur said:
(e-mail address removed):



use strict;
use warnings;

my ($a, $b, $c) = ( 0 ) x 3;

print "$a\t$b\t$c\n";
__END__

C:\Home\asu1> t
0 0 0

Or if you don't want to use a numerical constant:

$_ = 0 for my ( $a, $b, $c );


John
 
X

xhoster

Dan Jacobson said:
How does one write
my $a=0; my $b=0; my $c=0;
as compactly as possible (blanks OK)?
my ($a,$b,$c); $a=$b=$c=0;?
my ($a,$b,$c)=(0,0,0);?
But always I end up saying something over again.

I usually, but not always, find that if a script is declaring a whole bunch
of variables all in one place, that it is poorly designed in the first
place, because it is using poor scoping.
Under use strict, I've got a lot of variables to declare and
initialize to zero,

It is fairly rare that you need to unitialize a variable to zero
upon declaring it. It is generally only needed when you are keeping
a counter or a summation in which it is legal for there to be zero
occurences.

If you have already considered these thing and still need to do it this
way, well, my apologies for the tangent.

Xho
 
A

Ala Qumsieh

Dan said:
How does one write
my $a=0; my $b=0; my $c=0;
as compactly as possible (blanks OK)?
my ($a,$b,$c); $a=$b=$c=0;?
my ($a,$b,$c)=(0,0,0);?
But always I end up saying something over again.

My vote goes to:

my $a = my $b = my $c = 0;

--Ala
 
T

Tassilo v. Parseval

Also sprach Abigail:
A. Sinan Unur ([email protected]) wrote on MMMMCLX September
MCMXCIII in <URL:news:[email protected]>:
:} use strict;
:} use warnings;
:}
:} my ($a, $b, $c) = ( 0 ) x 3;


Yeah, but that requires you to count the number of variables.

This doesn't:

$_ = 0 for
my ($a, $b, $c);

Linguistically speaking, there is probably a slight pragmatic problem
with declaring variables in a statement-modifier in that the rather
irrelevant part (namely the initialization with zero) is up front. Your
formatting nicely avoids this problem but maybe costs a few fractions of
a brain-second on first reading it.


Tassilo
 
T

Tassilo v. Parseval

Also sprach Abigail:
Tassilo v. Parseval ([email protected]) wrote on
MMMMCLXI September MCMXCIII in <URL::} Also sprach Abigail:
:} > $_ = 0 for
:} > my ($a, $b, $c);
:}
:} Linguistically speaking, there is probably a slight pragmatic problem
:} with declaring variables in a statement-modifier in that the rather
:} irrelevant part (namely the initialization with zero) is up front. Your
:} formatting nicely avoids this problem but maybe costs a few fractions of
:} a brain-second on first reading it.


I hadn't thought about that. I purely did it for engineering reasons.
Formatting it this way means you can disable the initialization by just
deleting (or outcommenting) a single line. And you can add initialization
of an existing declaration by just adding a line above it.

Whereas I hadn't thought about what you just noted. I think it would be
a valuable addition to perlstyle.pod. Although I am aware that you can
declare and initialize variables that way, it is somewhat not part of my
every-day repertoire. I will have to rectify that.

Tassilo
 
B

Brian McCauley

Tad said:
my $a = my $b = my $c = 0;


Why do you feel the need to initialize them to zero?

And for that matter why do you think you want to declare them all in the
same place?

Generally you should always declare all variables in the smallest
applicable scope.

Generally you should use the natural representation for things - don't
use several separate variables for something that's logically a single
agregate.

If you are declaring three or more variables in the same place this is
usually (but not always) a sign that either you are declaring them in
the wrong place or that you should be using an agregate.
 
H

Heinrich Mislik

Or if you don't want to use a numerical constant:

$_ = 0 for my ( $a, $b, $c );

According to

# perl -MO=Deparse -e '$_ = 0 for my ($ , $b, $c);print "$a\n"'
;
foreach $_ (my($a, $b, $c)) {
$_ = 0;
}
print "$a\n";
-e syntax OK

this should not work, but ist does:

# perl -we '$_ = 0 for my ($a, $b, $c);print "$a\n"'
0

Strange that

# perl -MO=Deparse -e 'for (my ($a, $b, $c)){$_ = 0};print "$a\n"'
foreach $_ (my($a, $b, $c)) {
$_ = 0;
}
print "$a\n";
-e syntax OK

almost gives the same result, but does not work:

# perl -we 'for (my ($a, $b, $c)){$_ = 0};print "$a\n"'
Name "main::a" used only once: possible typo at -e line 1.
Use of uninitialized value in concatenation (.) or string at -e line 1.

Is this a problem of Deparse ?

Cheers

Heinrich
 
H

Heinrich Mislik

[email protected] says... said:
# perl -MO=Deparse -e '$_ = 0 for my ($ , $b, $c);print "$a\n"'

should of course be

# perl -MO=Deparse -e '$_ = 0 for my ($a, $b, $c);print "$a\n"'

And I forgot:

# perl -v

This is perl, v5.8.3 built for aix-64all

Copyright 1987-2003, Larry Wall
 
S

Shawn Corey

Heinrich said:
According to

# perl -MO=Deparse -e '$_ = 0 for my ($ , $b, $c);print "$a\n"'
;
foreach $_ (my($a, $b, $c)) {
$_ = 0;
}
print "$a\n";
-e syntax OK

this should not work, but ist does:

# perl -we '$_ = 0 for my ($a, $b, $c);print "$a\n"'
0

Strange that

# perl -MO=Deparse -e 'for (my ($a, $b, $c)){$_ = 0};print "$a\n"'
foreach $_ (my($a, $b, $c)) {
$_ = 0;
}
print "$a\n";
-e syntax OK

almost gives the same result, but does not work:

# perl -we 'for (my ($a, $b, $c)){$_ = 0};print "$a\n"'
Name "main::a" used only once: possible typo at -e line 1.
Use of uninitialized value in concatenation (.) or string at -e line 1.

Is this a problem of Deparse ?

Cheers

Heinrich

No, there is no problem with Deparse.

In this statement:
$_ = 0 for my ( $a, $b, $c );
the variables $a, $b, and $c are scoped outside the loop.

In:
for ( my( $a, $b, $c )){ $_ = 0 }
the variables are scoped inside the loop.

Try: perl -MO=Deparse -w -e 'for (my ($a, $b, $c)){$_ = 0};print "$a\n"'

BTW, I use:
my $a =0;
Then copy, paste, and change the name.

--- Shawn
 
W

William Goedicke

Dear Dan -

Dan> How does one write my $a=0; my $b=0; my $c=0; as compactly as
Dan> possible (blanks OK)?

I'm probably missing some potential gotcha' but, I've been successfully
using:

my( $a, $b, $c);

for years. This is, of course, only good for initializing to null/0.
It's worthy of note that it handles arrays and hashes as well as
scalars.

I'd be very interested in hearing what bugs this technique exposes me
to.

- Billy

============================================================
William Goedicke (e-mail address removed)
Cell 617-510-7244 http://www.goedsole.com:8080
============================================================

Lest we forget:

All big projects are completed through the execution of little
manageable steps.

- William Goedicke
 
A

Arndt Jonasson

William Goedicke said:
Dan> How does one write my $a=0; my $b=0; my $c=0; as compactly as
Dan> possible (blanks OK)?

I'm probably missing some potential gotcha' but, I've been successfully
using:

my( $a, $b, $c);

for years. This is, of course, only good for initializing to null/0.
It's worthy of note that it handles arrays and hashes as well as
scalars.

Initializing to 'undef', rather. "Null" does not have a specific meaning
in Perl, as far as I know (but I'm still fairly new in this group) (*);
and they are definitely not initialized to 0.
I'd be very interested in hearing what bugs this technique exposes me
to.

Probably few bugs by itself, but I suspect you do not use
use warnings;
in your code, because if you do, then I would guess (though I may be
wrong) you get warnings for uninitialized variables here and there,
when they are used numerically. And if you shut off warnings, you
miss a lot of warnings that really point to bugs.

This is the warning in question:
(W) An undefined value was used as if it were already defined. It was
interpreted as a "" or a 0, but maybe it was a mistake. To suppress this
warning assign an initial value to your variables.

(*) Actually, a search through the documentations seems to indicate
that "null" mostly means "empty", and sometimes "empty string". It's
also used for Nul (ASCII code 0), which I think is wrong, but maybe
that usage is more confusing in the C world, where there are null pointers
too.
 
A

A. Sinan Unur

Dear Dan -


Dan> How does one write my $a=0; my $b=0; my $c=0; as compactly as
Dan> possible (blanks OK)?

I'm probably missing some potential gotcha' but, I've been successfully
using:

my( $a, $b, $c);

for years. This is, of course, only good for initializing to null/0.

That initializes the variables to undef, not 0 or any other value.

IMNSHO, it is rarely necessary to initialize a whole list of variables to
the same value at the same time if one sticks with declaring variables at
the point of first use.

Sinan,
 
D

Dan Wilga

A. Sinan Unur said:
That initializes the variables to undef, not 0 or any other value.

Which is why I tend to use:

my( $a, $b, $c ) = ( 0, 0, 0 );

This syntax also makes it fairly obvious what's going on if you want to
use different values:

my( $a, $b, $c ) = ( 0, 100, $_[0] );

though I obviously wouldn't recommend it for clarity if you have more
than a few variables being assigned.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top