View Single Post
Old 10-10-2002, 07:36 AM   #2 (permalink)
gwartheg
Registered User
 
gwartheg's Avatar
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 8
gwartheg is on a distinguished road
Send a message via ICQ to gwartheg
Re: Working with Text Files

Quote:
Originally posted by rdove
My task requires me to write all my information to a text file. Now the next thing they want is for me to get certain information from the text file based on critera. Kind of like SQL in a database only I must use a text file.

Anyone have any great linksor advice that deals with searching through information in a text file with perl?

Thanks
Code Examples may be of some help. I like Perl Monks, too. In the mean time, is this what you're trying to do? Or is it more complicated?
Code:
open (IN, "file") or die "Can't read file: $!\n";
while ( <IN>) {
chomp;
my @fields = split;
    if ($fields[0] =~ /PATTERN/) {
        # do something
    }
}
close(IN);
Here's how you can simply get a list of all lines containing a certain pattern:
Code:
open (IN, "file") or die "Can't read file: $!\n";
my @lines = <IN>;
my @stuff = grep { /PATTERN/ } @lines;
print @stuff;
close(IN);
gwartheg is offline   Reply With Quote