Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Using Split function

by perldigious (Priest)
on Nov 27, 2019 at 21:43 UTC ( #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
Node Status?
node history
Node Type: note [id://11109328]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2020-06-04 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (30 votes). Check out past polls.

    Notices?