Conditional Statement failure --- if ($a>0)

A

Anno Siegel

Edward Wijaya said:
Hi all,

This are my code snippet.
The countArray subroutines are suppose to count
the number of elements withiin an array.

You don't need a subroutine for that. An array, used in scalar
context, returns the number of its elements, so

my $a = @b;

is all you need.
However the compilation gives the following error:

Use of uninitialized value in concatenation (.) or string at pvalue_v12.pl
line 325.
Use of uninitialized value in numeric gt (>) at pvalue_v12.pl line 327.

Those aren't compilation errors, they are run-time warnings.
Is there anything wrong with my conditional statement i.e, if ($a>0)
or is it because of my subroutines?
#----My code snippet-----
$a = countArray(@b);

You don't show what @b contains. Presumably it is empty.

Also, why isn't $a declared? You aren't running under strict, are you?
print "$a\n"; #----- line 325

if ($a> 0) { #-----line 327
bla bla bla;
}
else { die "bla bla bla"}


#----Subroutines----
sub countArray {
my @params = @_;
my $Key;
my $count;

That should be

my $count = 0;
foreach $Key (@params) {$count++}
return $count;
}

Apart from being unnecessary, the sub is (almost) correct. The problem
is that with an empty array the loop body is never executed, and hence
$count never is assigned a value. Just initialize $count to 0.

Anno
 
W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

Hi all,

This are my code snippet.
The countArray subroutines are suppose to count
the number of elements withiin an array.

However the compilation gives the following error:

Use of uninitialized value in concatenation (.) or string at pvalue_v12.pl
line 325.
Use of uninitialized value in numeric gt (>) at pvalue_v12.pl line 327.

Is there anything wrong with my conditional statement i.e, if ($a>0)
or is it because of my subroutines?

Thanks so much for your time.

Regards
Edward WIJAYA
SINGAPORE

#----My code snippet-----
$a = countArray(@b);
print "$a\n"; #----- line 325

if ($a> 0) { #-----line 327
bla bla bla;
}
else { die "bla bla bla"}


#----Subroutines----
sub countArray {
my @params = @_;
my $Key;
my $count;
my $count = 0;
foreach $Key (@params) {$count++}
return $count;
}

To count the number of elements in an array you might want to consider
doing the following :

$count = scalar @array;
 
T

Tad McClellan

Also, why isn't $a declared? You aren't running under strict, are you?


Maybe he is and maybe he isn't.

We can't tell since $a (and $b) happens to be exempted
from "use strict" anyway. :)
 
A

A. Sinan Unur

(in the subject line)
Re: Oops, it gives error only when "@b" is empty, thanks (eom)

Don't put your entire message in the subject line. Don't quote an entire
message if you are not going to say anything about the material you have
quoted.

Please pay attention to Tad's remark regarding the special variables $a and
$b (for more info, see perldoc -f sort).

Do stop using a sub to count the number of elements in an array.
 
A

Ala Qumsieh

Bernard said:
[...]


To count the number of elements in an array you might want to
consider doing the following :

$count = scalar @array;



To count the number of elements in an array *you* might want to
consider doing the following:


$count = @array;

Nothing wrong with using scalar() explicitly. Personally, I don't, but
some people find that clearer. I would *encourage* anybody to use it if
it helps their maintainability.

--Ala
 
A

Anno Siegel

Tad McClellan said:
Maybe he is and maybe he isn't.

We can't tell since $a (and $b) happens to be exempted
from "use strict" anyway. :)

Oh, right. I didn't notice whaqt particular variable went undeclared.

Anno
 
A

Anno Siegel

Edward Wijaya said:
Sorry for my "misbehavior" guys.

Please don't top-post. See the posting guidelines for this group, they
are posted here regularly.
BTW, I am not using $a under strict,
even if I declare $a=0 beforehand
it still gives the same error.

Of course it doesn't help to initialize $a, its value will be set to
whatever the sub returns.

It's the count variable in the sub that needs initialization.

[TOFU snipped]

Anno
 
A

Anno Siegel

Tad McClellan said:
Maybe he is and maybe he isn't.

We can't tell since $a (and $b) happens to be exempted
from "use strict" anyway. :)

Oh, right. I didn't notice what particular variable went undeclared.

Anno
 
A

Anno Siegel

Tad McClellan said:
Maybe he is and maybe he isn't.

We can't tell since $a (and $b) happens to be exempted
from "use strict" anyway. :)

Oh, right. I didn't notice waqt particular variable went undeclared.

Anno
 
J

Jim Keenan

Edward Wijaya said:
Sorry for my "misbehavior" guys.

BTW, I am not using $a under strict,
even if I declare $a=0 beforehand
it still gives the same error.

For more on why you shouldn't use $a and $b as user-defined variables:
perldoc -f sort

jimk
 
E

Edward Wijaya

Hi all,

This are my code snippet.
The countArray subroutines are suppose to count
the number of elements withiin an array.

However the compilation gives the following error:

Use of uninitialized value in concatenation (.) or string at pvalue_v12.pl
line 325.
Use of uninitialized value in numeric gt (>) at pvalue_v12.pl line 327.

Is there anything wrong with my conditional statement i.e, if ($a>0)
or is it because of my subroutines?

Thanks so much for your time.

Regards
Edward WIJAYA
SINGAPORE

#----My code snippet-----
$a = countArray(@b);
print "$a\n"; #----- line 325

if ($a> 0) { #-----line 327
bla bla bla;
}
else { die "bla bla bla"}


#----Subroutines----
sub countArray {
my @params = @_;
my $Key;
my $count;
foreach $Key (@params) {$count++}
return $count;
}
 
E

Edward Wijaya

Hi all,

This are my code snippet.
The countArray subroutines are suppose to count
the number of elements withiin an array.

However the compilation gives the following error:

Use of uninitialized value in concatenation (.) or string at
pvalue_v12.pl line 325.
Use of uninitialized value in numeric gt (>) at pvalue_v12.pl line 327.

Is there anything wrong with my conditional statement i.e, if ($a>0)
or is it because of my subroutines?

Thanks so much for your time.

Regards
Edward WIJAYA
SINGAPORE

#----My code snippet-----
$a = countArray(@b);
print "$a\n"; #----- line 325

if ($a> 0) { #-----line 327
bla bla bla;
}
else { die "bla bla bla"}


#----Subroutines----
sub countArray {
my @params = @_;
my $Key;
my $count;
foreach $Key (@params) {$count++}
return $count;
}
 
E

Edward Wijaya

Sorry for my "misbehavior" guys.

BTW, I am not using $a under strict,
even if I declare $a=0 beforehand
it still gives the same error.

Why is it harmful to use sub for counting arrays' content?

Thanks
Edward WIJAYA
Singapore
 

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,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top