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