Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

I want to search through an asci file

Posted on 2000-03-14
4
Medium Priority
?
208 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 90 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
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…
Six Sigma Control Plans

972 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