By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.


P: 16

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
May 21 '09 #1
Share this Question
Share on Google+
11 Replies

Expert 100+
P: 1,275
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?

May 22 '09 #2

Expert 100+
P: 174
what have you tried so far? lets see some code.
May 22 '09 #3

P: 16
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
  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),
  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. }
thanks for patience
May 22 '09 #4

Expert 100+
P: 410
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. }

P.S: Please use code tags while posting code in future
May 22 '09 #5

P: 16
my falt,

thanks a lot,

i just started programming these last few days,
May 22 '09 #6

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

P: 16
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;
  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"; 
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
May 22 '09 #8

Expert Mod 2.5K+
P: 3,503
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;
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.


May 22 '09 #9

P: 16
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;
  9. #searching for a key word inside the txt file
  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;
  24. }
May 26 '09 #10

Expert 100+
P: 410
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. }
May 26 '09 #11

P: 16
great man, thanks a lot
May 26 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.