Solved

I want to search through an asci file

Posted on 2000-03-14
4
158 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>> My file is very big. Is this a problem?  No

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

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now