simple way to test for undef

Discussion in 'Perl Misc' started by ktom, Jun 26, 2003.

  1. ktom

    ktom Guest

    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
    ktom, Jun 26, 2003
    #1
    1. Advertising

  2. ktom wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Jun 26, 2003
    #2
    1. Advertising

  3. ktom

    Jay Tilton Guest

    ktom <> wrote:

    : 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.
    Jay Tilton, Jun 26, 2003
    #3
  4. ktom <> wrote:

    > 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. :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 26, 2003
    #4
  5. ktom

    ktom Guest

    Tad McClellan wrote:
    > ktom <> wrote:
    >
    >
    >>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.
    >
    >
    >>am i making harder than it needs to be.

    >
    >
    >
    > I think the answer to that should be clear by now. :)
    >
    >
    ktom, Jun 27, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vikas Yadav
    Replies:
    0
    Views:
    1,115
    Vikas Yadav
    Sep 9, 2003
  2. Sachin

    #undef for function

    Sachin, Nov 25, 2005, in forum: C Programming
    Replies:
    3
    Views:
    413
    Peter Ammon
    Nov 28, 2005
  3. How to undef a typedef??

    , Nov 16, 2006, in forum: C Programming
    Replies:
    15
    Views:
    1,291
    Kenneth Brody
    Nov 18, 2006
  4. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    680
    Skybuck Flying
    Oct 4, 2009
  5. Tim McDaniel

    undef($foo) versus $foo = undef()?

    Tim McDaniel, Aug 19, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    136
    Peter J. Holzer
    Aug 19, 2009
Loading...

Share This Page