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;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|