On Jul 16, Rahul Garg said:
>1)I want to look out for a particular string in a file.
> Actually there is only one strng in each line of a file.
> How to search for it.............sample code........as i am new to syntax of perl
For just finding ONE string in ANOTHER string, the index() function is all
you need.
my $pos = index("jeffrey", "fre"); # 3
my $pos = index("jeffrey", "jeff"); # 0
my $pos = index("jeffrey", "free"); # -1
As you can see, the function returns -1 on failure. Please read the docs
for the function:
perldoc -f index
>2)I want to look out for a particular part(match) in a string in a file.
> Actually there is only one string in each line of a file.
You need to open the file, and go through it, line by line, executing the
index function on the line.
while (<FILE>) {
if (index($_, $word) > -1) {
# $word was found somewhere in $_
}
}
Learn how to work with files:
perldoc perlopentut
perldoc -f open
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]