Solved

I want to search through an asci file

Posted on 2000-03-14
4
186 Views
Last Modified: 2010-03-05
I'm a new programmer in Perl. I have a ascii file with 20000 records. How i can make searches inside this file with one or two fields. Which are the steps?
May i have a simple example of this type of program.
Thank you
0
Comment
Question by:vage78
  • 3
4 Comments
 
LVL 5

Expert Comment

by:PC_User321
ID: 2615128
Is each line of the file divided into fields?
If so, what is used to separate the fields?

Here is an example where the fields are separated with a '|' character.
Assume you want the first field to be "Person" and the third field to be "Every month":

$Record0Search = "Person";
$Record2Search = "Every month";

open(FILE, "data.txt");
while ($Line = <FILE>) {
   @Fields = split '\|', $Line;
   print $Line if (($Fields[0] eq $Record0Search) && ($Fields[2] eq $Record2Search));
}

0
 
LVL 5

Accepted Solution

by:
PC_User321 earned 30 total points
ID: 2615182
The 'split' command divides the lines into fields, separated by the character within the single quotes.

The 'eq' in the comparison means that the fields must be exactly equal the the values you are looking for.  Other comparisons are possible, such a case-insentive comparisons, or checking to see if the field _contains_ the string you are searching for.
http://www.perl.com/pub/doc/manual/html/pod/perlre.html gives you details.
For example, if you just wanted to find lines containing words "seven" or "eight" (case insensitive) in any field you would replace the print line with
   print $Line if ($Line =~ /seven|eight/i/);
(and you would not need to do the 'split')

I hope this helps.
0
 

Author Comment

by:vage78
ID: 2615208
Thank you very much.I will try.My file is very big. Is this a problem? (20000 records) or everything is ok.
May i use also indexes.
Thanks a lot
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2615210
>> My file is very big. Is this a problem?  No

>> May i use also indexes?
Please explain what you mean.

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question