### map and undef - odd behaviour with an array of arrays

by Melly (Hermit)
 on Dec 22, 2012 at 08:26 UTC Need Help??
Melly has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monkees

The following one liner prints, rather unexpectedly, 'still defined' - any ideas as to what's going on?

```perl -e "\$x[0]=[0];map{undef \$_}(@x);print 'still defined' if defined
+@x;"
```map{\$a=1-\$_/10;map{\$d=\$a;\$e=\$b=\$_/20-2;map{(\$d,\$e)=(2*\$d*\$e+\$a,\$e**2
-\$d**2+\$b);\$c=\$d**2+\$e**2>4?\$d=8:_}1..50;print\$c}0..59;print\$/}0..20
Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk

Replies are listed 'Best First'.
Re: map and undef - odd behaviour with an array of arrays
by eyepopslikeamosquito (Chancellor) on Dec 22, 2012 at 08:38 UTC

Running:

```use Data::Dumper;
\$x[0]=[0];map{undef \$_}(@x);print "still defined\n" if defined @x;
print Dumper(\@x);
produces:
```defined(@array) is deprecated at undef1.pl line 2.
(Maybe you should just omit the defined()?)
still defined
\$VAR1 = [
undef
];
I trust that clarifies what is going on. That is, your array contains a single element with the value undef. Note that an array containing a single element with an undef value is not undef, not even false in scalar context. To be false in scalar context, the array needs to be empty.

Oh, and please heed the deprecated warning and replace "if defined @x" with simply "if @x" (see defined for why).

But why did map{undef \$_}(@x) behave differently than undef @x?

Edit to add - oh, I'm being stupid - it's undefining each element rather than @x. When I was testing this, for some reason I concluded that perl -e "@x=qw(a);map{undef \$_}(@x);print 1 if defined @x;" didn't print 1 (but it does).

One of those days...

```map{\$a=1-\$_/10;map{\$d=\$a;\$e=\$b=\$_/20-2;map{(\$d,\$e)=(2*\$d*\$e+\$a,\$e**2
-\$d**2+\$b);\$c=\$d**2+\$e**2>4?\$d=8:_}1..50;print\$c}0..59;print\$/}0..20
Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk

But why did map{undef \$_}(@x) behave differently than undef @x?
"undef @x" empties the array while "map{undef \$_}@x" sets each element in the array to the value undef. Since your array had one element in it, that element was not deleted, rather its value was set to undef.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009983]
Approved by Athanasius
help
Chatterbox?
 Discipulus without Corion and with choroba in another timezone the monastery was silent.. I was silbing alone in the cloister [choroba]: Yes, I feel alone in the cyberspace... [choroba]: Not many occasions to talk to my wife, either :-( Discipulus s/silbing/whistle/ dunno from where this word was in my mind.. [Discipulus]: lost in time in space and in the meaning (rocky horror picture show) [Corion]: Discipulus: Yeah, a short vacation on the Canary islands (Lanzarote). Always great to be in the warmth and sun, from a cold and rainy February here ;) [Corion]: choroba: Yeah, you have to find a time for the regular skype (or whatever) meetup, I imagine that's hard

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2017-02-22 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Before electricity was invented, what was the Electric Eel called?

Results (325 votes). Check out past polls.