473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

FILEHANDLE Questions

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
May 21 '09 #1
11 3045
gpraghuram
1,275 Expert 1GB
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
May 22 '09 #2
Icecrack
174 Expert 100+
what have you tried so far? lets see some code.
May 22 '09 #3
Sevla
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
  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
May 22 '09 #4
nithinpes
410 Expert 256MB
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
May 22 '09 #5
Sevla
16
my falt,

thanks a lot,

i just started programming these last few days,
May 22 '09 #6
Sevla
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
Sevla
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;
  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
May 22 '09 #8
numberwhun
3,509 Expert Mod 2GB
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
May 22 '09 #9
Sevla
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;
  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. }
May 26 '09 #10
nithinpes
410 Expert 256MB
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.  
May 26 '09 #11
Sevla
16
great man, thanks a lot
May 26 '09 #12

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

Similar topics

2
by: Jorge Godoy | last post by:
Hi! I'm trying to get a specific information from inside an image and it works correctly on unices. What I do is: 1. associate a filehandle with the image file 2. get the desired line 3....
1
by: Eduard W. Lohmann | last post by:
Hello. I write this little thing to help me log from several instances of the same class in apache, mod_perl. But I can't figure one thing out. package Logger; require Carp; # Write the...
2
by: Bill | last post by:
I'm trying to use a hash key as a filehandle like so. #!/usr/local/bin/perl use strict; my %buf = ( 'F00' => 'foo.dat' ); &open_files();
0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
1
by: Zairay | last post by:
Hi All, I'm having a problem with the Shell32.dll when I try to open a program from my Access db. When I try to open a program called FalconViewLite from my access database I get an error in...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
7
by: freshpetals | last post by:
Hello people..I just wanted to know that can we store number of files in a filehandle ..if yes how could we do that???I did write a program but its just parsing result from the file which I'm giving...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.