HOW: Check string existence in a file.

M

mrlawrencelam

Seeing my questin below and you will know that I don't know much about
Perl. But I urgently need a simple Perl script that do this task:

How do I write a little Perl script to read a file and check for a
string?

1) Read the file "aaa.txt".
2) If string "xxx yyy zzz" is in that file, then do action 1.
3) If not, then do action 2.

Thanks.
 
E

Eric Schwartz

Seeing my questin below and you will know that I don't know much about
Perl. But I urgently need a simple Perl script that do this task:

This newsgroup is not for writing Perl programs for people (though you
may luck out and someone will do it anyway, because they're
sufficiently bored). It's about discussing Perl programs. So please
read the documentation first, and try and write some code; we're much
more inclined to help someone who's made a little effort than someone
who says, "I need you to write my code for me, for free, fast!".
How do I write a little Perl script to read a file and check for a
string?

1) Read the file "aaa.txt".

perldoc -f open
perldoc perlop (search for "I/O Operators")
2) If string "xxx yyy zzz" is in that file, then do action 1.
3) If not, then do action 2.

perldoc perlsyn

I hope these documentation references are helpful.

-=Eric
 
J

John W. Krahn

Seeing my questin below and you will know that I don't know much about
Perl. But I urgently need a simple Perl script that do this task:

How do I write a little Perl script to read a file and check for a
string?

1) Read the file "aaa.txt".
2) If string "xxx yyy zzz" is in that file, then do action 1.
3) If not, then do action 2.


*UNTESTED*

#!/usr/bin/perl
use warnings;
use strict;

my $file = 'aaa.txt';
open my $fh, '<', $file or die "Cannot open $file: $!";

my $found;
while ( <$fh> ) {
$found += /xxx yyy zzz/;
}

if ( $found ) {
do_action_1();
}
else {
do_action_2();
}

__END__



John
 
W

William James

How do I write a little Perl script to read a file and check for a
string?

1) Read the file "aaa.txt".
2) If string "xxx yyy zzz" is in that file, then do action 1.
3) If not, then do action 2.

In Ruby:

found = false
open("aaa.txt"){ |filehandle|
filehandle.each{ |line|
found = true and break if line.index("xxx yyy zzz") }
}
if found
puts 'yes'
else
puts 'no'
end
 
M

Mark Clements

William said:
In Ruby:

found = false
open("aaa.txt"){ |filehandle|
filehandle.each{ |line|
found = true and break if line.index("xxx yyy zzz") }
}
if found
puts 'yes'
else
puts 'no'
end
This has no relevance to the question. There are hundreds of languages
out there, each of which may or may not have advantages or disadvantages
over Perl in certain scenarios, but giving solutions to Perl questions
using whatever happens to be somebody's pet language isn't very helpful.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top