Connecting Tech Pros Worldwide Help | Site Map

FILEHANDLE Questions

Newbie
 
Join Date: May 2009
Posts: 16
#1: May 21 '09
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
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,256
#2: May 22 '09

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
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#3: May 22 '09

re: FILEHANDLE Questions


what have you tried so far? lets see some code.
Newbie
 
Join Date: May 2009
Posts: 16
#4: May 22 '09

re: FILEHANDLE Questions


thats a simple example, im trying to make a searching by a list name, looking for a specific word or name,

Expand|Select|Wrap|Line Numbers
  1. #!C:/perl/bin/perl.exe
  2.  
  3. @list_name = (carlos,jonas,bruno,jonas,milles,cassio,otario,
  4. felipe,loko,anta,paulo,roberto,jose,pablo,srv,zeraldo,eduardo,
  5. caio,kaua,diego,fabio,pedro,helbert,geraldo,jonathan,linus,
  6. theo,ken,monstro,otaciolio,tonhao,beto,dartanhan,joe,jimi),
  7.  
  8. print "Do you want search for a name?\n";
  9. $choose=<STDIN>; chomp $choose;
  10. if($choose =~ /[Y,y]es|ok/) {
  11. print " which one\?\n"};
  12. $choose=<STDIN>; chomp $choose;
  13. foreach(grep(/@_/,@list_name)) {
  14. print "results:$_\n";
  15. }
  16.  
thanks for patience
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#5: May 22 '09

re: FILEHANDLE Questions


That is a logical mistake. The pattern for grep should be $choose:
Expand|Select|Wrap|Line Numbers
  1. foreach(grep(/$choose/,@list_name)) {
  2. print "results:$_\n";
  3. }
  4.  

P.S: Please use code tags while posting code in future
Newbie
 
Join Date: May 2009
Posts: 16
#6: May 22 '09

re: FILEHANDLE Questions


my falt,

thanks a lot,

i just started programming these last few days,
Newbie
 
Join Date: May 2009
Posts: 16
#7: May 22 '09

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
#8: May 22 '09

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
Expand|Select|Wrap|Line Numbers
  1. #!C:/perl/bin/perl.exe
  2. open (my $read, "c:/perl/notice.txt" )|| die "failed to open notice.txt $!\n";
  3. while(<$read>) {
  4. print "$_";
  5. }
  6. close $read;
  7.  
  8. print "search a word\?\n";
  9. $choose=<STDIN>; chomp $choose;
  10. if($choose =~ /[yes|ok]/) {
  11. print "digit a word\n"};
  12. $choose=<STDIN>; chomp $choose;
  13. foreach(grep(/$choose/,my $read)) { 
  14. print "result:my $read\n"; 
  15.  
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
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,565
#9: May 22 '09

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:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
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
#10: May 26 '09

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

Expand|Select|Wrap|Line Numbers
  1. #!C:/perl/bin/perl.exe
  2. #filehandle, opening
  3. start:
  4. open FILE, "c:/perl/noticia.txt" || die "failed to open notice.txt $!\n";
  5. @read=<FILE>;   # read entire file into array
  6. close FILE;
  7. chomp @read;
  8.  
  9. #searching for a key word inside the txt file
  10.  
  11. print "search a word\?\n";
  12. $choose=<STDIN>; chomp $choose;
  13. if($choose =~ /yes|ok/) {
  14.         print "enter a word\n";
  15.         $choose=<STDIN>; chomp $choose;
  16.         foreach(grep(/$choose/,@read)) {
  17.                 print "result: $_\n";
  18.         }
  19. }
  20. else{
  21.     print "invalid\n";
  22.     goto start;
  23.  
  24. }
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#11: May 26 '09

re: FILEHANDLE Questions


You can save the result of grep into an array and then run the test.
Expand|Select|Wrap|Line Numbers
  1. my @res = grep(/$choose/,@read);
  2. print "STRING NOT FOUND!!!" unless(@res); 
  3. foreach(@res) {
  4. print "result: $_\n";
  5. }
  6.  
Newbie
 
Join Date: May 2009
Posts: 16
#12: May 26 '09

re: FILEHANDLE Questions


great man, thanks a lot
Reply