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

Push a tab-delimited line into an array

by koolgirl (Hermit)
on Sep 28, 2011 at 13:52 UTC ( #928327=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks :D,

OK, I'm parsing a log file, to match specific lines for billing. Each line has six values (date/time, IP, user name, user id, id number, and message). Each element is tab delimited.

So, my question would be if I store each line into an array, is it going to store six elements per line because of the tab delimiters, or will each line be an element?, as that is how I'm assigning it:

push(@array, $line)

I know this is a basic question, **ducks to avoid the llama book flying at head**, but I've been stuck in web development for a while, so I just don't remember and I searched this question and I all seem to get is documents on split.

UPDATE:

As long as I'm asking ridiculous questions, might as well include them all together, so I'm looking through the indexes of O'Reilly's books (I have on CD), and I'm seeing passing variables to subroutines as strings:

&sub($foo, $bar)

If I passed an array to &sub, would it be put into the @_ by element, as $foo and $bar would, or will it all end up in $_[0]?


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man.." -- George Bernard Shaw

Replies are listed 'Best First'.
Re: Push a tab-delimited line into an array
by MidLifeXis (Monsignor) on Sep 28, 2011 at 14:01 UTC

    See split. That is a correct path to take. You could also see some of the CSV modules on CPAN.

    Update: re your update, perhaps perlsub might help.

    --MidLifeXis

      WOW. I guess I need a break. Some sleep..a slap in the face...


      "The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man.." -- George Bernard Shaw

Re: Push a tab-delimited line into an array
by davido (Cardinal) on Sep 28, 2011 at 15:26 UTC

    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!


    Dave

Re: Push a tab-delimited line into an array
by rnaeye (Friar) on Sep 28, 2011 at 14:52 UTC

    When I am not sure what the element is in an array, I use  shift or  pop function to test it. You can do the same thing and see what is removed from the array.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2022-10-05 22:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (25 votes). Check out past polls.

    Notices?