[LIP] Re: [LIH]finding position of a match in perl
Raj Mathur <[email protected]>
| Newsgroups | gmane.user-groups.linux.india.programmers |
|---|---|
| Message-ID | <[email protected]> |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[Switching to Linux-India-Programmers]
>>>>> "Gowtham" == Gowthaman R <Gowthaman> writes:
>> Probably many ways of doing it, one may be:
>> m/(.*)(matchre)(.*)(matchre)(.*)/ $1 will contain the first
>> (unmatched) string, $2 the first match, $3 the next unmatched
>> string, etc. length will give you the lengths of the
>> (un)matched strings.
Gowtham> thank you raj, yes! i can just find length of variable
Gowtham> S`. But slows down script. it takes too much time to work
Gowtham> on 1000s of strings. I have to run this many times a day.
Gowtham> i am trying the following code ... is that ok ....i have
Gowtham> to implement it yet .. $var1 ="ABCDEFGHSDFD...."; while
Gowtham> ($var =~m/ABC/g) { printf "ABC is at %d\n", pos($var)-2;
Gowtham> }
If you're searching for constant strings then what you're doing would
be fine. Just remove the matched part of $var before the next search:
$var1 = "...." ;
while ( $var =~ /ABC/ )
{
print "ABC is at " , pos ( $var ) - 2 , "\n" ;
$var = substr $var , pos ( $var ) ;
}
Something like that should do the trick.
Regards,
- -- Raju
- --
Raj Mathur [email protected] http://kandalaya.org/
GPG: 78D4 FC67 367F 40E2 0DD5 0FEF C968 D0EF CC68 D17F
It is the mind that moves
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 and Gnu Privacy Guard <http://www.gnupg.org/>
iD8DBQE/8bJyyWjQ78xo0X8RAu3sAKCGSatiyO2kuaPUMQ3kEjhhMJGszwCgk4x8
lYQOYCfvkl5XyvgsMbQ8Mi8=
=nEMA
-----END PGP SIGNATURE-----
-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
Free Linux Tutorials. Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click