Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > saying my $a=0; my $b=0; more compactly

Reply
Thread Tools

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

 
 
Dan Jacobson
Guest
Posts: n/a
 
      01-20-2005
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()...
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-20-2005
Dan Jacobson <(E-Mail Removed)> wrote in news:87oefjlu5y.fsf_-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> 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
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-21-2005
Dan Jacobson <(E-Mail Removed)> wrote:
> 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?


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-21-2005
A. Sinan Unur wrote:
> Dan Jacobson <(E-Mail Removed)> wrote in news:87oefjlu5y.fsf_-
> (E-Mail Removed):
>
>
>>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


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

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


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      01-21-2005
Dan Jacobson <(E-Mail Removed)> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      01-21-2005
Dan Jacobson wrote:
> 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
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      01-21-2005
Also sprach Abigail:

> A. Sinan Unur ((E-Mail Removed)) wrote on MMMMCLX September
> MCMXCIII in <URL:news:Xns95E4B56EE9CE4asu1cornelledu@132.236.5 6.8>:


>:} 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
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      01-21-2005
Also sprach Abigail:

> Tassilo v. Parseval ((E-Mail Removed)) wrote on
> MMMMCLXI September MCMXCIII in <URL:news:slrncv16ln.pk.tassilo.von.parseval@local host.localdomain>:
>:} 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
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      01-21-2005


Tad McClellan wrote:

> Dan Jacobson <(E-Mail Removed)> wrote:
>
>>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?


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.

 
Reply With Quote
 
Heinrich Mislik
Guest
Posts: n/a
 
      01-21-2005
In article <UDYHd.6338$Qb.1542@edtnps89>, (E-Mail Removed) says...
>
>
>A. Sinan Unur wrote:
>> Dan Jacobson <(E-Mail Removed)> wrote in news:87oefjlu5y.fsf_-
>> (E-Mail Removed):
>>
>>
>>>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

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

--
Heinrich Mislik
Zentraler Informatikdienst der Universitaet Wien
A-1010 Wien, Universitaetsstrasse 7
Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140

 
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
Realizing memdup() compactly in C++ Nordl÷w C++ 8 05-16-2009 08:55 AM
Error in tag lib : saying "unable to find setter method for attribute" callme sanjay via JavaKB.com Java 5 04-17-2007 08:58 AM
Saying you're an MSCE when you're not =?Utf-8?B?QW5keSBTbWl0aA==?= Microsoft Certification 13 10-18-2005 02:39 PM
Re: I get error saying the library is not registered Alexey Smirnov ASP .Net 0 02-26-2004 01:19 AM
usebean not working, Tomcat saying cannot find class Nawab Java 0 02-19-2004 12:45 AM



Advertisments