Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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;

In reply to Re: With substr, index the offset don't get proper start position by pgmer6809
in thread With substr, index the offset don't get proper start position by Oskym

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (7)
    As of 2019-05-22 17:52 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!