Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

why do I have to provide context to split result in a separate statement?

by Mark_Galeck (Novice)
on Sep 21, 2011 at 00:38 UTC ( #927040=perlquestion: print w/replies, xml ) Need Help??
Mark_Galeck has asked for the wisdom of the Perl Monks concerning the following question:

Hello, why does this bomb
print @{\(@arr = split / /, '')}
while this does not
@arr = split / /, ''; print @{\@arr}
Thank you, Mark

Replies are listed 'Best First'.
Re: why do I have to provide context to split result in a separate statement?
by jwkrahn (Monsignor) on Sep 21, 2011 at 01:40 UTC

    \(@arr = split / /, '') returns a list of references.

    For example \( $a, $b, $c ) is actually ( \$a, \$b, \$c ) and you can't dereference that as an array.

    What you could do is copy the list to an anonymous array:

    print @{[@arr = split / /, '']}
      Don't need to copy it into two arrays.
      print @{[ split / /, '' ]}
        Thank you very much!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927040]
Approved by koolgirl
help
Chatterbox?
[GrandFather]: It's either lines that your browser won't split (no white space) or nodes with <pre> tags which ought to be code tags
[GrandFather]: I haven't noticed the problem for a while, but it can be hard to find the nasty node. If you do find it, consider it and with luck a janitor will fix it

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-05-24 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?