Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Push a tab-delimited line into an array

by davido (Cardinal)
on Sep 28, 2011 at 15:26 UTC ( #928353=note: print w/replies, xml ) Need Help??

in reply to Push a tab-delimited line into an array

push @array, $line will not "auto-split" the line into individual elements, so @array would receive just one element. If you use push( @array, split( /]s*,\s*/, $line ) ) you would be splitting $line into its own temporary array and then pushing all the elements onto @array. That would work if the none of the legitimate elements could somehow have a comma embedded in them. More robust CSV parsing is available from modules on CPAN.

Regarding the update: subroutine( $foo, $bar ) is just about the same as @array = ( $foo, $bar ); subroutine( @array ); The difference is that in the first case, @_ would be aliased to $foo and $bar, where in the second case it would be aliased to @array. So if you were to modify $_[0] in the first example, you would be modifying the contents of $foo, whereas in the second example you would be modifying the contents of $array[0]. Don't use the & unless you are either wielding sub references, or understand that it has a subtle effect on how @_ is passed. In other words, you probably don't need to use it.

Have fun!


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-11-28 22:06 GMT
Find Nodes?
    Voting Booth?