http://www.perlmonks.org?node_id=1231537


in reply to With substr, index the offset don't get proper start position

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.