subroutine only returns non zero sums

R

Rocky

hi all,
this subroutine was written originally without the if/else statement for
$total. It returned any non-zero sum perfectly but whenever @number was
all zeros it came back blank(undefined?) What did I miss? It works now
but it soesn't seem to be the best way. Thank you.
Rocky

sub addnums {
my @number = @_;
my $total;
my $digit;
foreach $digit (@number)
{
$total += $digit;
}
if (defined($total))
{
$total = $total;
}
else
{$total = 0;}
return $total;
}
 
G

Glenn Jackman

Rocky said:
hi all,
this subroutine was written originally without the if/else statement for
$total. It returned any non-zero sum perfectly but whenever @number was
all zeros it came back blank(undefined?) What did I miss? It works now

Initialize $total when you declare it:
sub addnums {
my $total = 0;
$total += $_ foreach @_;
return $total;
}
 
G

Gunnar Hjalmarsson

Rocky said:
this subroutine was written originally without the if/else
statement for $total. It returned any non-zero sum perfectly but
whenever @number was all zeros it came back blank(undefined?) What
did I miss?

Are you asking what you missed in some code you don't show?
It works now but it soesn't seem to be the best way.

Well, it can be shortened:

sub addnums {
my $total = 0;
$total += $_ for @_;
$total
}
 
B

Brian McCauley

Rocky said:
this subroutine was written originally without the if/else statement for
$total. It returned any non-zero sum perfectly but whenever @number was
all zeros it came back blank(undefined?) What did I miss?

Initialisation of $total to 0 before the loop.
It works now but it soesn't seem to be the best way. Thank you.
Rocky

sub addnums {
my @number = @_;

Why copy @_ ?
my $total;

Here is where you should have initialised $total.
my $digit;
foreach $digit (@number)

Don't do that. Perl will see that $digit in the foreach is currently
in scope as a lexical and implicitly redeclare a new lexical $digit
scoped to the loop. The $digit in the outer lexical scope is never
used. You only want the one scoped to the lood. So say instead:

foreach my $digit (@number)
{
$total += $digit;
}
if (defined($total))
{
$total = $total;
}
else
{$total = 0;}
return $total;
}

Oh and some indentation would be good.

sub addnums {
my $total = 0;
my $digit;
foreach my $digit (@_)
{
$total += $digit;
}
return $total;
}

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
R

Richard Morse

Glenn Jackman said:
Initialize $total when you declare it:
sub addnums {
my $total = 0;
$total += $_ foreach @_;
return $total;
}

Would it be valid to make it even shorter by doing:

sub addnums {
my $total += $_ foreach @_;
return $total || 0;
}

?

I don't think that the return statement would be a problem, but I'm
wondering if the "don't lexicalize variables with statement modifiers"
rule applies here, or only with 'if'/'unless' statement modifiers?

Thanks,
Ricky
 
U

Uri Guttman

RM> Would it be valid to make it even shorter by doing:

RM> sub addnums {
RM> my $total += $_ foreach @_;
RM> return $total || 0;
RM> }

RM> I don't think that the return statement would be a problem, but I'm
RM> wondering if the "don't lexicalize variables with statement modifiers"
RM> rule applies here, or only with 'if'/'unless' statement modifiers?

have you tried it? that is the first rule about asking questions like
this. you have code and does it work?

try it with and without my and report any differences. you will find the
answer you seek.

uri
 
B

Brian McCauley

Richard Morse said:
my $total += $_ foreach @_;

AFAIK the behaviour of a my() declaration with a statement qualifier
is explicitly undefined. So the above may happen do what you want
today but may not in future.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
G

gnari

Brian McCauley said:
AFAIK the behaviour of a my() declaration with a statement qualifier
is explicitly undefined. So the above may happen do what you want
today but may not in future.

and in fact, it probably does NOT happen to do what
you want today.

gnari
 
R

Richard Morse

RM> sub addnums {
RM> my $total += $_ foreach @_;
RM> return $total || 0;
RM> }

RM> I don't think that the return statement would be a problem, but I'm
RM> wondering if the "don't lexicalize variables with statement modifiers"
RM> rule applies here, or only with 'if'/'unless' statement modifiers?

have you tried it? that is the first rule about asking questions like
this. you have code and does it work?

try it with and without my and report any differences. you will find the
answer you seek.[/QUOTE]

Now feeling properly chastised (both by Mr. Guttman and Mr. McClellan),
I have done this test and discovered it will not work.

Ricky
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top