Hi,

Thanks for this.

I am trying to read out the 6 characters following "DosID" in a longer string into a new variable ($dosid). Here is the code using the position. As the position is not always the same (sometimes it starts at 81, sometimes at 44, etc.), I have to use another if-clause to correct the value. This can get rather bothersome, if the position jumps around all the time.

foreach $input (@input) { if (($input=~'DosID=') && ($count=~/[02468]$/)) { $count2++; $dosid=substr($input,81,6); if ($dosid=~'detai') { $dosid=substr($input,44,6); } if ($dosid=~'"') { chop($dosid); } }
The two count variables ($count, $count2) are counting the lines of the input file and the number of lines containing the search string "DosID" respectively. I am only interested in every second occurence.

I tried it with $', but this seems to pick up the value of one of the count variables instead of the remainder of the string.

Another thing: I would like not having to specify the number of characters of the substring. Instead I would like to use a delimiter as a stop signal (in my example: a " character should delimit the substring). I could use a loop checking every character and then appending it to my variable, but I thought there might be a more elegant way of doing things.

I guess I am looking for a function like substr which includes regular expressions and if-clauses instead of fixed parameters. Thus, the start of the substring would be defined as "begin after" and the end as "stop before".


In reply to Re^2: Alternative to Substr? by New Novice
in thread Alternative to Substr? by New Novice

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, details, 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, summary, 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.