simple way to test for undef

K

ktom

i have created an array that is sparse, ie there are a number of
elements which are undefined, because i have used a numeric index to the
array and not all of the intermediate indices get used.

here is the code to print this array, which doesn't work...

print Dumper( @ca ) ;
foreach my $item ( @ca ) {
if( defined ref $ca[$item] ) {
print "chain $item has $ca[$item] exceptions\n";
}
}


here is a snippet of the output..

$VAR1 = 6;
$VAR2 = undef;
$VAR3 = undef;
$VAR4 = 5;
$VAR5 = undef;
$VAR6 = 88;
$VAR7 = 1;
chain 6 has 1 exceptions
Use of uninitialized value in array element at ./findViolInScanPath.pl
line 51, <IFILE> line 100.


it seems the autovivification process is biting me, but i don't know how
to work around it. am i making harder than it needs to be.

thanks.

kevin
 
J

John W. Krahn

ktom said:
i have created an array that is sparse, ie there are a number of
elements which are undefined, because i have used a numeric index to the
array and not all of the intermediate indices get used.

here is the code to print this array, which doesn't work...

print Dumper( @ca ) ;
foreach my $item ( @ca ) {
if( defined ref $ca[$item] ) {

The return value of ref() is true or false but not undef. Your
statement will always print even if $item or $ca[$item] is undefined
(one of which is producing the warning message.)

print "chain $item has $ca[$item] exceptions\n";
}
}


John
 
J

Jay Tilton

: i have created an array that is sparse, ie there are a number of
: elements which are undefined, because i have used a numeric index to the
: array and not all of the intermediate indices get used.
:
: here is the code to print this array, which doesn't work...
:
: foreach my $item ( @ca ) {
: if( defined ref $ca[$item] ) {

Why the ref()? Are you expecting the elements of @ca to be something
besides ordinary scalars?

Why test the return from ref() for defined-ness? ref() is documented
to return a false value if its argument is not a reference, but
'false' and 'undefined' are not interchangable.

: print "chain $item has $ca[$item] exceptions\n";

Maybe you're shooting for something more like this:

if( defined $item && defined $ca[$item] ) {
print "chain $item has $ca[$item] exceptions\n";
}

: it seems the autovivification process is biting me,

No autovivification is happening.
 
T

Tad McClellan

ktom said:
Subject: simple way to test for undef


Testing for undef has nothing to do with your problem.

foreach my $item ( @ca ) {
if( defined ref $ca[$item] ) {


$item gets the _values_ from @ca, you seem to be expecting it to
get the _indexes_ of the values. So you want either:

foreach my $item ( @ca ) { # preferred
if( defined ref $item ) {

or

foreach my $index ( 0 .. $#ca ) {
if( defined ref $ca[$index] ) {

am i making harder than it needs to be.


I think the answer to that should be clear by now. :)
 
K

ktom

Tad said:
Subject: simple way to test for undef



Testing for undef has nothing to do with your problem.


foreach my $item ( @ca ) {
if( defined ref $ca[$item] ) {



$item gets the _values_ from @ca, you seem to be expecting it to
get the _indexes_ of the values. So you want either:

foreach my $item ( @ca ) { # preferred
if( defined ref $item ) {

or

foreach my $index ( 0 .. $#ca ) {
if( defined ref $ca[$index] ) {

i found this to work..

foreach my $index ( 0..$#ca ) {
if( defined $ca[$index] ) {
print "chain $index has $ca[$index] exceptions\n";
}
}

with an output of..

$VAR1 = 22;
$VAR2 = undef;
$VAR3 = 1;
$VAR4 = 5;
$VAR5 = 4;
$VAR6 = 141;
$VAR7 = 15;
chain 0 has 22 exceptions
chain 2 has 1 exceptions
chain 3 has 5 exceptions
chain 4 has 4 exceptions
chain 5 has 141 exceptions
chain 6 has 15 exceptions

also, converting to a hash and using keys works too.

thanks for your help.
 

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,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top