Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.

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

In reply to Re: Tutorial suggestion: split and join by gmax
in thread Tutorial suggestion: split and join by davido

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2014-12-27 14:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (177 votes), past polls