Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by hippo (Canon)
on Mar 21, 2019 at 10:55 UTC ( #1231529=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2019-05-22 23:05 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 22:53 UTC) Welcome new users!