FILEHANDLE Questions | Newbie | | Join Date: May 2009
Posts: 16
| | |
hello
im novice perl programmer, and i need a little help about manipulating files, as text.txt for example,
i need seek some specific "word" or information on the txt file or any external file and display an error log if the word/info was not found, such as creating a log file reporting with details
and i have not known the code to do this yet, Thanks for patience |  | Expert | | Join Date: Mar 2007 Location: Chennai
Posts: 1,256
| | | re: FILEHANDLE Questions
If u need to search for a pattern and display an error then why cant u use grep from a shell script and do it?
raghu
|  | Expert | | Join Date: Sep 2008 Location: Sydney, Australia
Posts: 173
| | | re: FILEHANDLE Questions
what have you tried so far? lets see some code.
| | Newbie | | Join Date: May 2009
Posts: 16
| | | re: FILEHANDLE Questions
thats a simple example, im trying to make a searching by a list name, looking for a specific word or name, -
#!C:/perl/bin/perl.exe
-
-
@list_name = (carlos,jonas,bruno,jonas,milles,cassio,otario,
-
felipe,loko,anta,paulo,roberto,jose,pablo,srv,zeraldo,eduardo,
-
caio,kaua,diego,fabio,pedro,helbert,geraldo,jonathan,linus,
-
theo,ken,monstro,otaciolio,tonhao,beto,dartanhan,joe,jimi),
-
-
print "Do you want search for a name?\n";
-
$choose=<STDIN>; chomp $choose;
-
if($choose =~ /[Y,y]es|ok/) {
-
print " which one\?\n"};
-
$choose=<STDIN>; chomp $choose;
-
foreach(grep(/@_/,@list_name)) {
-
print "results:$_\n";
-
}
-
thanks for patience
|  | Expert | | Join Date: Dec 2007
Posts: 400
| | | re: FILEHANDLE Questions
That is a logical mistake. The pattern for grep should be $choose: -
foreach(grep(/$choose/,@list_name)) {
-
print "results:$_\n";
-
}
-
P.S: Please use code tags while posting code in future
| | Newbie | | Join Date: May 2009
Posts: 16
| | | re: FILEHANDLE Questions
my falt,
thanks a lot,
i just started programming these last few days,
| | Newbie | | Join Date: May 2009
Posts: 16
| | | re: FILEHANDLE Questions
the last question about this topic...
with external files as text.txt i can use this code?
by replace the @list_name for open ( $file "c:....../file.txt")|| die "failed to open text.txt $!\n"; ??
thanks for your kind patience
| | Newbie | | Join Date: May 2009
Posts: 16
| | | re: FILEHANDLE Questions
can someone help me with code to get a specific word, from a text.txt
here is the code, i cant find where is mistake -
#!C:/perl/bin/perl.exe
-
open (my $read, "c:/perl/notice.txt" )|| die "failed to open notice.txt $!\n";
-
while(<$read>) {
-
print "$_";
-
}
-
close $read;
-
-
print "search a word\?\n";
-
$choose=<STDIN>; chomp $choose;
-
if($choose =~ /[yes|ok]/) {
-
print "digit a word\n"};
-
$choose=<STDIN>; chomp $choose;
-
foreach(grep(/$choose/,my $read)) {
-
print "result:my $read\n";
-
}
-
obs: this is a veery simple code, i didnt complete the conditions as you can see , my focus is to make the perl get a word from the text.txt and display it on prompt as i asked
|  | Site Moderator | | Join Date: May 2007 Location: New Hampshire
Posts: 2,565
| | | re: FILEHANDLE Questions
I think that the first thing you really need to learn is to include the following two pragmas after your shebang line: -
use strict;
-
use warnings;
-
They will help alleviate all of the typical coding mistakes that are made, especially by beginners. I know a lot of people who won't look at your code unless you do use them, so it is highly advised.
Also, it was mentioned to you earlier in this thread and I am now saying it. Please use code tags. They are required around any code you include in the forums and if you don't add them, then we have to clean up behind you.
Thanks!
Jeff
| | Newbie | | Join Date: May 2009
Posts: 16
| | | re: FILEHANDLE Questions
hello guys, i solved the searching topic, i just need a help about how make a error message if your searched word were not found - #!C:/perl/bin/perl.exe
-
#filehandle, opening
-
start:
-
open FILE, "c:/perl/noticia.txt" || die "failed to open notice.txt $!\n";
-
@read=<FILE>; # read entire file into array
-
close FILE;
-
chomp @read;
-
-
#searching for a key word inside the txt file
-
-
print "search a word\?\n";
-
$choose=<STDIN>; chomp $choose;
-
if($choose =~ /yes|ok/) {
-
print "enter a word\n";
-
$choose=<STDIN>; chomp $choose;
-
foreach(grep(/$choose/,@read)) {
-
print "result: $_\n";
-
}
-
}
-
else{
-
print "invalid\n";
-
goto start;
-
-
}
|  | Expert | | Join Date: Dec 2007
Posts: 400
| | | re: FILEHANDLE Questions
You can save the result of grep into an array and then run the test. -
my @res = grep(/$choose/,@read);
-
print "STRING NOT FOUND!!!" unless(@res);
-
foreach(@res) {
-
print "result: $_\n";
-
}
-
| | Newbie | | Join Date: May 2009
Posts: 16
| | | re: FILEHANDLE Questions
great man, thanks a lot
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|