Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Using Split function

by perldigious (Priest)
on Nov 27, 2019 at 21:43 UTC ( [id://11109328]=note: print w/replies, xml ) Need Help??


in reply to Using Split function

You have a simple syntax error in your call of the built in split function.

use strict; use warnings; my $line = "This is :a line :with colons :in it"; my @items = split /:/, $line; print "$_\n" foreach (@items);

If you are working through Learning Perl, Chapter 9 explains some basic usage of split.

If you are like me and forget syntax easily, try to remember that things like split and print are built in functions of Perl and as such should be treated like any subroutine call where you wrote the subroutine (just no "&" ever necessary, see Chapter 4 of Learning Perl :-)). You can even wrap parenthesis around the list (list implies comma separators) of arguments you are passing them for clarity... e.g.

my @items = split(/:/, $line); print('just ', 'like ', 'this ', ':-)')

EDIT: Also, if you are only looking to extract certain items of your list for each line, and you know their index locations in the array you split them in to, you may want to take a look at array slices which are covered in Chapter 17 of Learning Perl.

Just another Perl hooker - My clients appreciate that I keep my code clean but my comments dirty.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-23 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found