CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
About file handling

 



Yuvraj
Deleted

Nov 23, 2000, 7:26 PM

Post #1 of 2 (806 views)
About file handling Can't Post

Hi,
I have a file which contains some words (one word in one line).I want to read it's contents and do some pattern matching.
But the problem here is when i read the file like
@list=<handle>;
the array list has all the words but a carriage return along with it.I used chop function then also a lot of error.

open(han,"f1.txt");
@list=<han>;
$i=0;
while($i<=$#list)
{

if("word"=~ /$list[$i]/)
{
print "right";
}

}


the f1.txt contains contents in this format
letter
word
sentence
paragraph

Will you please help me to solve this problem.The main problem is that i can't compare the strings.It may be due to the format in which the array stores the file contents.
Thank you in advance





perlplexer
Deleted

Nov 24, 2000, 5:24 AM

Post #2 of 2 (806 views)
Re: About file handling [In reply to] Can't Post

 
open FH, "<file.txt" or die "from: $!\n";
chomp(@lines = <FH> );
foreach $line (@lines){
if ($line =~ /word/i){
print "right\n";
}
}
close FH;

or a slightly shorter version:

open FH, "<file.txt" or die "from: $!\n";
chomp(@lines = <FH> );
for (@lines){
print "right\n" if (/word/i);
}
close FH;

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives