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

Discussion in 'Perl Misc' started by Dan Jacobson, Jan 20, 2005.

  1. Dan Jacobson

    Dan Jacobson Guest

    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()...
     
    Dan Jacobson, Jan 20, 2005
    #1
    1. Advertising

  2. Dan Jacobson <> wrote in news:87oefjlu5y.fsf_-
    :

    > 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
     
    A. Sinan Unur, Jan 20, 2005
    #2
    1. Advertising

  3. Dan Jacobson <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 21, 2005
    #3
  4. A. Sinan Unur wrote:
    > Dan Jacobson <> wrote in news:87oefjlu5y.fsf_-
    > :
    >
    >
    >>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
     
    John W. Krahn, Jan 21, 2005
    #4
  5. Dan Jacobson

    Guest

    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.


    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
     
    , Jan 21, 2005
    #5
  6. Dan Jacobson

    Ala Qumsieh Guest

    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
     
    Ala Qumsieh, Jan 21, 2005
    #6
  7. Also sprach Abigail:

    > A. Sinan Unur () wrote on MMMMCLX September
    > MCMXCIII in <URL:news:Xns95E4B56EE9CE4asu1cornelledu@132.236.56.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$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Jan 21, 2005
    #7
  8. Also sprach Abigail:

    > Tassilo v. Parseval () wrote on
    > MMMMCLXI September MCMXCIII in <URL:news:>:
    >:} 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$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Jan 21, 2005
    #8
  9. Tad McClellan wrote:

    > Dan Jacobson <> 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.
     
    Brian McCauley, Jan 21, 2005
    #9
  10. In article <UDYHd.6338$Qb.1542@edtnps89>, says...
    >
    >
    >A. Sinan Unur wrote:
    >> Dan Jacobson <> wrote in news:87oefjlu5y.fsf_-
    >> :
    >>
    >>
    >>>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
     
    Heinrich Mislik, Jan 21, 2005
    #10
  11. In article <41f0dbbf$0$11352$>, says...

    Sorry, this line

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

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
     
    Heinrich Mislik, Jan 21, 2005
    #11
  12. Dan Jacobson

    Shawn Corey Guest

    Heinrich Mislik wrote:
    >
    > 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
     
    Shawn Corey, Jan 21, 2005
    #12
  13. Dear Dan -

    >>>>> "Dan" == Dan Jacobson <> writes:


    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
    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
     
    William Goedicke, Jan 21, 2005
    #13
  14. William Goedicke <> writes:
    > >>>>> "Dan" == Dan Jacobson <> writes:

    >
    > 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.
     
    Arndt Jonasson, Jan 21, 2005
    #14
  15. William Goedicke <> wrote in
    news::

    > Dear Dan -
    >
    >>>>>> "Dan" == Dan Jacobson <> writes:

    >
    > 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,
     
    A. Sinan Unur, Jan 21, 2005
    #15
  16. Dan Jacobson

    Dan Wilga Guest

    In article <Xns95E5693CBA241asu1cornelledu@132.236.56.8>,
    "A. Sinan Unur" <> wrote:

    > William Goedicke <> wrote in
    > news::
    >
    > > 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.


    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.

    --
    Dan Wilga
    ** Remove the -MUNGE in my address to reply **
     
    Dan Wilga, Jan 21, 2005
    #16
    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. Alexey Smirnov
    Replies:
    0
    Views:
    1,047
    Alexey Smirnov
    Feb 26, 2004
  2. Nawab
    Replies:
    0
    Views:
    633
    Nawab
    Feb 19, 2004
  3. callme sanjay via JavaKB.com
    Replies:
    5
    Views:
    4,362
    haraul
    Apr 17, 2007
  4. -
    Replies:
    3
    Views:
    413
  5. Nordlöw
    Replies:
    8
    Views:
    1,296
    James Kanze
    May 16, 2009
Loading...

Share This Page