Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: undef'ing @arrays caveat

by lestrrat (Deacon)
on Apr 20, 2002 at 01:28 UTC ( #160719=note: print w/replies, xml ) Need Help??


in reply to undef'ing @arrays caveat

The trick here is that setting @array to undef doesn't really destroy or 'undefine' that array variable.

When you think about it though, why would an assignment undefine the value of an array? When you're doing assignment, you're doing exactly that: assignment ;)

But this following which your post inspired me to try out is, I gotta say, weird:

perl -MData::Dumper -e '%hash="foo"; print Dumper( \%hash )' # produces this... ( so far so good... ) # $VAR1 = { # 'foo' => undef # }; perl -MData::Dumper -e '%hash = undef; print Dumper( \%hash )' # produces: # $VAR1 = { # '' => undef # };

Replies are listed 'Best First'.
Re: Re: undef'ing @arrays caveat
by particle (Vicar) on Apr 20, 2002 at 02:22 UTC
    i don't find that so strange weird. in each case, the assignment to hash assumed a second argument of undef. it's as if you assigned %hash=undef,undef;. perl just helps you do what you mean. as in the post above, list context is assumed due to the type of the lvalue of the assignment operation.

    however, %hash=undef=>undef; may not do what you mean, if you don't know what you mean. here, the difference between , and => is plain.

    # $VAR1 = { # 'undef' => undef # };

    p.s. assignment is pretty much guaranteed to do just that--assign something. if you want to assign an empty hash, you'll have to assign one %hash = ()

    ~Particle ;о

      Oh I see, the '' is the stringified undef. Yes, that I appreciate, but

      however, %hash=undef=>undef; may not do what you mean, if you don't know what you mean. here, the difference between , and => is plain.

      Boy, did I sound *that* clueless? I was just verifying the various somevariable = undef behavior before I post an answer, and just came upon one that seemed weird strange. ugh.

        i'm sorry, that bit wasn't meant for you. that was meant for all readers. i should have been more clear, perhaps by posting that bit to the original poster. sorry for any unintended offense :-)

        ~Particle ;о

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-08-14 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (153 votes). Check out past polls.

    Notices?