FAQ
Hi all,

I write a small script for permutation. When I use
version 1) Perl always complaint it although I get the
result. If I use version 2) it never says a word about
it. Any comments?

Thank you in advance,

Li

####version 1)###

use strict;
use warnings;

print permutation(5,2); # 5 choose 2

exit;

#subroutine permutation

sub permutation{

my ($n, $k)=@_;
my $result=1;

for ($k; $k>=1;$k--) {$result*=$n--;}#line 15
return $result;
}

#####screen output####
Useless use of private variable in void context at
fac1.pl line 15.
20


####version 2)###

use strict;
use warnings;

print permutation(5,2); # 5 choose 2

exit;

#######subroutine permutation##########

sub permutation{

my $n=shift;
my $result=1;

for (my $k=shift; $k>=1;$k--) {$result*=$n--;}
return $result;
}

###screen output####

20

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Search Discussions

  • Igor Sutton at Sep 28, 2006 at 1:06 pm
    for ($k; $k>=1;$k--) {$result*=$n--;}#line 15
    You don't need to declare $k inside for (). Change that to

    for (; $k >= 1; $k--)

    and perl won't complain about it anymore. I think the place before the first
    ';' is used for declaring any variables inside the lexical scope of for.
    Maybe someone could explain this better?

    --
    Igor Sutton Lopes <igor.sutton@gmail.com>
  • Chen li at Sep 28, 2006 at 3:18 pm
    Thank you very much,

    Li

    --- Igor Sutton wrote:
    for ($k; $k>=1;$k--) {$result*=$n--;}#line 15
    You don't need to declare $k inside for (). Change
    that to

    for (; $k >= 1; $k--)

    and perl won't complain about it anymore. I think
    the place before the first
    ';' is used for declaring any variables inside the
    lexical scope of for.
    Maybe someone could explain this better?

    --
    Igor Sutton Lopes <igor.sutton@gmail.com>

    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • Adriano Ferreira at Sep 28, 2006 at 1:47 pm

    On 9/28/06, chen li wrote:
    Hi all,

    I write a small script for permutation. When I use
    version 1) Perl always complaint it although I get the
    result. If I use version 2) it never says a word about
    it. Any comments?
    Perl always complain (provided you called for that with -w or "use
    warnings") when you do something like that:

    $ perl -w -e 'my $a; $a'
    Useless use of a variable in void context at -e line 1

    This is meant to warn you about a probable mistake:
    you may have meant
    $a++
    a # to invoke a sub
    or something like that. As Igor has said the first statement in the
    for is often used for declaring loop variables or, more generally, for
    loop initialization statements. If you don't have these initialization
    statements, you may omit it.

    Regards,
    Adriano Ferreira
  • Chen li at Sep 28, 2006 at 3:19 pm
    Hi Andriano,

    Thank you very much,

    Li

    --- Adriano Ferreira wrote:
    On 9/28/06, chen li wrote:
    Hi all,

    I write a small script for permutation. When I use
    version 1) Perl always complaint it although I get the
    result. If I use version 2) it never says a word about
    it. Any comments?
    Perl always complain (provided you called for that
    with -w or "use
    warnings") when you do something like that:

    $ perl -w -e 'my $a; $a'
    Useless use of a variable in void context at -e line
    1

    This is meant to warn you about a probable mistake:
    you may have meant
    $a++
    a # to invoke a sub
    or something like that. As Igor has said the first
    statement in the
    for is often used for declaring loop variables or,
    more generally, for
    loop initialization statements. If you don't have
    these initialization
    statements, you may omit it.

    Regards,
    Adriano Ferreira

    --
    To unsubscribe, e-mail:
    beginners-unsubscribe@perl.org
    For additional commands, e-mail:
    beginners-help@perl.org
    <http://learn.perl.org/>
    <http://learn.perl.org/first-response>


    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • Dr.Ruud at Sep 28, 2006 at 2:16 pm

    chen li schreef:

    for ($k; $k>=1;$k--) {$result*=$n--;}
    Alternative:

    $result *= $n-- for 1..$k ;

    --
    Affijn, Ruud

    "Gewoon is een tijger."
  • Chen li at Sep 28, 2006 at 3:17 pm
    Thanks,

    Li

    --- "Dr.Ruud" wrote:
    chen li schreef:
    for ($k; $k>=1;$k--) {$result*=$n--;}
    Alternative:

    $result *= $n-- for 1..$k ;

    --
    Affijn, Ruud

    "Gewoon is een tijger."



    --
    To unsubscribe, e-mail:
    beginners-unsubscribe@perl.org
    For additional commands, e-mail:
    beginners-help@perl.org
    <http://learn.perl.org/>
    <http://learn.perl.org/first-response>


    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 28, '06 at 12:43p
activeSep 28, '06 at 3:19p
posts7
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase