Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Tutorial suggestion: split and join

by gmax (Abbot)
on Aug 28, 2003 at 22:54 UTC ( #287545=note: print w/replies, xml ) Need Help??

in reply to Tutorial suggestion: split and join

It is not only the intermediate elements. The effect of a delimiter are felt on the empty elements at the beginning and the end of the source string.

Consider the following examples

#!/usr/bin/perl -w use strict; my $line = " Bart Lisa Maggie Marge Homer "; # notice the leading and trailing spaces my @simpsons; for ( " ", '\s', '\s+' ) { print "delimiter /$_/\n"; @simpsons = split ( /$_/, $line ); print map {"<$_>"} @simpsons; print $/; } print "delimiter ' '\n"; @simpsons = split ( ' ', $line ); print map {"<$_>"} @simpsons; print $/; __END__ delimiter / / <><><Bart><><Lisa><Maggie><Marge><Homer> delimiter /\s/ <><><Bart><><Lisa><Maggie><Marge><Homer> delimiter /\s+/ <><Bart><Lisa><Maggie><Marge><Homer> delimiter ' ' <Bart><Lisa><Maggie><Marge><Homer>

The best choice if you want to split a string by spaces and you don't want the empty elements is to use a simple quoted space (not a regex) as a delimiter, as the last example shows.

From perldoc -f split

As a special case, specifying a PATTERN of space ("' '") will split on white space just as "split" with no arguments does. Thus, "split(' ')" can be used to emulate awk's default behavior, whereas "split(/ /)" will give you as many null initial fields as there are leading spaces. A "split" on "/\s+/" is like a "split(' ')" except that any leading whitespace produces a null first field. A "split" with no arguments really does a "split(' ', $_)" internally.

Update If you want to document the above behavior, you can use B::Deparse.

perl -MO=Deparse -e '$_=" a b c ";print map {"<$_>"} split' $_ = ' a b c '; print map({"<$_>";} split(" ", $_, 0));

However, this will work in Perl 5.8.0 but not in 5.6.1. (in 5.6.1 the output of the one-liner is correct, but the deparsed code is not). Apparently, there was a bug that was recently fixed. Thanks to diotalevi for his useful analysis in this matter.

 _  _ _  _  
(_|| | |(_|><

Replies are listed 'Best First'.
Re: Re: Tutorial suggestion: split and join
by davido (Cardinal) on Aug 28, 2003 at 23:23 UTC
    Thanks for the comment. I've made a few updates to the original node to see if I can do a better job of emphasizing your point. It's my goal to further revise it based on comments here.

    My whole reason for going through re-writing the split and join section of the tutorial was just that I felt the existing section didn't provide enough depth and examples to give a true feel of the power, flexibility, and usefulness of split and join. Rather than to re-invent the tutorial, I just hoped to tighten the spokes of the existing wheel. It is ok if this just spurrs a discussion that might lead to an improvement here and there. Split/Join seemed like a good place to start.

    If you (or anyone else) have suggestions on how it could be further improved I'd like to hear. Maybe in the end someone will read it and come away with a better feel for how to use split because of our efforts.

    In keeping with Perlmonks tradition, I'll list the updates made to the node, and who helped make them possible. Rather than clutter up the cohesiveness of the node, the update and credit list will appear at the bottom, documenting what input led to assorted improvements.


    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2022-12-05 21:52 GMT
Find Nodes?
    Voting Booth?

    No recent polls found