Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

creating array refs

by agoth (Chaplain)
on Aug 21, 2000 at 22:09 UTC ( [id://28864]=perlquestion: print w/replies, xml ) Need Help??

agoth has asked for the wisdom of the Perl Monks concerning the following question:

I thought that \( list ) and [ list ] did the same thing but apparently not,
Can someone explain why the first structure below works while the second doesnt?
@ary = ('item1:value1:text1', 'item2:value2:text2'); for (@ary) { push @flibble, [ split /:/, $_ ]; } for (@ary) { push @flibble, \(split /:/, $_ ); } # then for (@flibble) { print "row : @{ $_ } \n"; # to test }

Thanks.

Replies are listed 'Best First'.
RE: creating array refs
by Boogman (Scribe) on Aug 21, 2000 at 22:32 UTC
    \( element1, element2, ... ) takes a reference to each element in the list and returns that list (i.e. ( \element1, \element2, \... ) whereas [ element1, element2, ... ] returns a reference to an array containing those elements.

    When you push the results of taking a refererence to a list into @fibble, you add a reference to each element in the list individually, as opposed to adding a reference to an array containing those elements as you do using the anonymous array composers [].
Re: creating array refs
by btrott (Parson) on Aug 21, 2000 at 22:19 UTC
    \(list) isn't valid syntax. And that's what you're doing with split, because split returns a list. You can't take a reference to a list; you can only use a list to build an anonymous array ref.

    \@array is valid syntax, because you can take a reference to an array.

    The difference is, LIST creates a new anonymous array reference and fills it with LIST; \@ARRAY takes a reference to an existing array.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-20 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found