Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: if (defined @arr) problem

by secret (Beadle)
on Dec 15, 2005 at 10:07 UTC ( [id://516905]=note: print w/replies, xml ) Need Help??


in reply to if (defined @arr) problem

That's because the array contains one value : 'undef' .
Look at what happens if you do :
sub test { return () ; }
You can also print scalar @a to see the what's the length of the array .

Replies are listed 'Best First'.
Re^2: if (defined @arr) problem
by bart (Canon) on Dec 15, 2005 at 10:40 UTC
    That's because the array contains one value : 'undef' .
    That looks like a string with the text "undef" to me. That's not what the array contains. Instead, it holds an undefined scalar value.

    Look at what happens if you do :
    sub test { return () ; }

    Lo and behold, it works!

    Still, testing definedness of an array is a very bad idea. Try running this code first:

    @a = qw(a b c);
    and now you'll see, the array will be defined (and empty).

    Don't. Do. It.

    Definedness of an array is nothing a newbie should ever be concerned about.

        Still, testing definedness of an array is a very bad idea. Try running this code first:
        @a = qw(a b c);
      Ah yes :) that's very true !  scalar @array is, of course, much better !

      ( i did meant the undef value, btw, and not the string 'undef' but it's true that my use of single quotes was indeed a bad idea !! )

      I'll add something : suppose the OP wanted to undef arrays in a sub . I think I would pass the array as a reference, undef it in the sub and return the reference .

        I'll add something : suppose the OP wanted to undef arrays in a sub . I think I would pass the array as a reference, undef it in the sub and return the reference.

        In technical discussions terms have precise meanings and it's important to use them in the same way that other people do. I don't think that "undef an array" means what you think it means.

        If you mean to remove all of the elements of the array, then say that. If you want to replace all of the elements with the value undef then say that. Or do you actually mean that you want to call undef @ARRAY? According to the docs that will "forget @ARRAY ever existed". Which of these do you mean?

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        Ah yes :) that's very true ! scalar @array is, of course, much better !
        And while we're here, it's worth reminding that if you use @array in scalar context, then an explicit call to scalar is not needed:
        D'Oh unless @array; # Valid Perl!!
      > Still, testing definedness of an array is a very bad idea.
      > Try running this code first: 
      >
      > @a = qw(a b c);
      >
      > and now you'll see, the array will be defined (and empty).
      

      Hmm, i get "defined: yes" and "size: 3" ...

      @a = qw/a b c/; printf "defined: %s\nsize: %d\n" , defined @a ? 'yes' : 'no' , scalar @a ;

      ... what did i miss?

        You missed plugging it into the whole script. I said "do this first", didn't I?

        Well, apparently that wasn't clear, so here is the whole current script, as I see it.

        #! /usr/bin/perl @a = qw/a b c/; @a = test(); printf "defined: %s\nsize: %d\n" , defined @a ? 'yes' : 'no' , scalar +@a ; sub test { return () ; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://516905]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found