Hi,
I was trying some perl regex and got a bit stuck while trying to print index values in ranges. Have a look at my code.
#!/iusr/bin/perl
open(FH, $ARGV[0]) || die("Cannot Open:$!");
while(<FH>){
$char = '-';
$offset = 0;
$result = index($_, $char, $offset);
while ($result != -1) {
print "Found $char at $result\n";
$offset = $result + 1;
$result = index($string, $char, $offset);
}
}
This code prints the location of "-" wherever it finds... e,g
A-----B
if it finds this character it will print the location 5 times.
found at 2
found at 3
found at 4
found at 5
found at 6
I wanted to print the range when it finds this type of case.
Means
found at 2 to 6
Any suggestion ?
Thanks in advance
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.