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.
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.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); } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |