http://qs321.pair.com?node_id=1231628


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;