473,378 Members | 1,138 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,378 software developers and data experts.

Searching a file for text

So far I have a script that attempts to guess your age (after you give it your name) with random numbers, no necessarily effective but it does the job. What I'm currently trying to do is modify the script so that it will write your name to a file and save the age in corolation to the name so that in the future when someone puts in their name if the name is already in there it'll search for the name and pull up the age linked to it. My question (for the moment) is how do I get Perl to search for the name in the file? I'll put up the script that this originated with so you can possibly understand better.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. print "Hello, I am Betrayal and may I ask your name?\n";
  4. $name = <STDIN>;
  5.     chop $name;
  6. print "Alright then $name I shall guess your age!\n";
  7.  
  8. $y = "n";
  9.  
  10. while($y =~ /n/) {
  11.     $guess = int(rand(50));
  12.     print "Are you $guess? <Y/N>\n";
  13.     $y = <STDIN>;}
  14.  
  15. if($y =~ /y/) {print "I win!\n"}
  16.  
Jul 7 '08 #1
2 1080
KevinADC
4,059 Expert 2GB
To write to a file look into the open() function. To read through a file and find some data you will also use open() to open the file and "while" to read it line by line to find what you want:

Expand|Select|Wrap|Line Numbers
  1. open(FH , 'file.txt') or die "$!";
  2. while (<FH>) {
  3.    if (/frank/) {
  4.       you found it so do something
  5.    }
  6. }
  7. close FH;
That is not exactly how you should do it. It depends on how you store the name and age in the file. Look up the open() function for now.
Jul 8 '08 #2
Thank you for the advice hope it helps =]]
Jul 8 '08 #3

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

Similar topics

3
by: hivie | last post by:
I have a problem that is causing me problems. I have a text file that stores 5 lines of crap (stuff that I dont need( for the user only)). After that there is data that is in three columns...
4
by: Michi | last post by:
I was wondering what the best solution is for making large numbers of TEXT (or BLOB?) fields searchable. For example, if I have a forum, what is the best way to be able to search for specific...
3
by: Paul H | last post by:
I have a text file that contains the following: ******************** __StartCustomerID_41 Name: Fred Smith Address: 57 Pew Road Croydon
1
by: Bud Dean | last post by:
I need to search files for given text. In particular, I'm searching dll's, exe's, asp, aspx and html pages. I am having difficulty converting the byte arrays to strings. The following code...
2
by: angus | last post by:
hello everybody, i'm having some problems searching a text file and then deleting the line. since i used fileopen(), i can use the eof() function to loop through the file, searching for the...
5
by: robinsiebler | last post by:
I have a batch of files that I am trying to search for specific text in a specific format. Each file contains several items I want to search for. Here is a snippet from the file: .......
7
by: pbd22 | last post by:
Hi. I am somewhat new to this and would like some advice. I want to search my xml file using "keyword" search and return results based on "proximity matching" - in other words, since the search...
4
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.