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


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

If you really want to do it using substr and index then here is one iterative method:

use strict; use warnings; my $line='PSAPPSRV.26476584 (6) 01/02/19 06:30:30 GetCertificate(3) Re +turning context. ID=PTWEBSERVER, Lang=ENG'; my $SpaceChar = ' '; my $result = $line; for (1..4) { $result = substr ($result, index ($result, $SpaceChar) + 1); print "$result\n"; }

Here's a simpler one with split instead:

#!/usr/bin/env perl use strict; use warnings; my $line='PSAPPSRV.26476584 (6) 01/02/19 06:30:30 GetCertificate(3) Re +turning context. ID=PTWEBSERVER, Lang=ENG'; my $SpaceChar = ' '; my $result = (split ($SpaceChar, $line, 5))[4]; print "$result\n";

Update: edit of first snippet to remove expensive no-op.