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

comment on

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

Brother hippo's answer is fine, but it may seem a little like magic to you, so I'm adding a bit of an explanation (if you're interested).

The split function takes a number of arguments; you can give it zero to three arguments. That information is available using perldoc -f split, or by looking the function up in whichever Perl book that you have.

The first argument is a regular expression -- in this case, we're using a space to split the string (the second argument) into pieces using the space as the splitting point. The third argument says "Stop splitting after this many pieces, and put the remaining string into the last list element".

So we're splitting your string into five pieces; and the last piece is whatever's after the fourth space.

Because this is Perl, there's more than one way to do it (we even abbreviate that to TMTOWTDI, pronounced TimToady), and you can use a loop if you like -- but hippo's solution using split is more Perl-ish.

You could even use a regular expression to capture the piece you want .. but that's left as an exercise for the student. Or someone else later on in this thread. :)

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.


In reply to Re: With substr, index the offset don't get proper start position by talexb
in thread With substr, index the offset don't get proper start position by Oskym

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 having an uproarious good time at the Monastery: (4)
    As of 2019-05-22 23:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you enjoy 3D movies?



      Results (140 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 18:53 UTC) Welcome new users!