Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by pgmer6809 (Novice)
on Mar 24, 2019 at 21:43 UTC ( #1231628=note: print w/replies, xml ) Need Help??


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

I think your basic error was to try to put too much into the variable offset. You are asking it to do too much. Here is a more verbose version showing which variables do what. We are not worried about shortest code here! An "experienced" Perl programmer would have used a regular expression to parse out the piece he wanted. See the last lines of the code below.

my $line='PSAPPSRV.26476584 (6) 01/02/19 06:30:30 GetCertificate(3) Re +turning context. ID=PTWEBSERVER, Lang=ENG'; my $SpaceChar = ' '; my $offset = 0; my $start_at=0; my $space_ctr = 0; my $result; while ($space_ctr < 4) { $offset = index($line, $SpaceChar, $start_at); # $offset $result = substr $line,index($line, $SpaceChar, $start_at); $space_ctr++; $start_at = $offset + 1; print "Substr($offset) = $result \n"; print "space #:$space_ctr found at offset: $offset \n"; } # using a regex to parse out the piece we want: $line =~ m/\S*\s+\S*\s+\S*\s+\S*\s+(.*)/ ; # \S* is zero or more NON space chars. \s+ is one o +r more SPACE chars. (.*) is everything after the 4th space. saved in +$1 for later use. my $wanted_substr = $1; print "Wanted String= $wanted_substr \n"; exit 1;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2019-07-22 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (22 votes). Check out past polls.

    Notices?