Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Understandig on Creating Empty array reference

by jesuashok (Curate)
on Dec 16, 2005 at 09:17 UTC ( #517176=perlquestion: print w/replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi All monks

I have a doubt in creating a empty array reference.

#!/usr/local/bin/perl + @d = ( 1,2); $array = \@d; $array = \(); $$array[0] = 1; + print "Value :", $$array[0] , "\n";
In the above code

If I comment "$array = \@d;" then If I run the code I am getting an error like

<bold>Not an ARRAY reference</bold>

Can't I create Empty array reference.

Or am I am making any mistake in understanding the array reference

Please adive me on the same.

Thank you all

"Keep pouring your ideas"

Replies are listed 'Best First'.
Re: Understandig on Creating Empty array reference
by Corion (Pope) on Dec 16, 2005 at 09:21 UTC

    \() does not create an array reference. See perldoc perlop on the \ operator to see what it does.

    What you want is:

    $array = []

Re: Understandig on Creating Empty array reference
by davido (Archbishop) on Dec 16, 2005 at 09:23 UTC

    A doubt would be tendancy toward disbelief. What you have is a question.

    $array = \(); doesn't create a reference to an empty array. You probably want $array = [];

    \() creates individual references to each entity inside of the (). Since there is no entity inside of your (), no reference is created. [] is the anonymous array constructor. That's what you're looking for.


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://517176]
Approved by BrowserUk
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2016-12-06 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (116 votes). Check out past polls.