Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: With substr, index the offset don't get proper start position

by talexb (Canon)
on Mar 21, 2019 at 12:47 UTC ( #1231537=note: print w/replies, xml ) Need Help??

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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2020-01-26 03:39 GMT
Find Nodes?
    Voting Booth?