Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-03-2002, 04:37 AM   #1 (permalink)
rdove
Masked Moderator
 
rdove's Avatar
 
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
rdove is on a distinguished road
Working with Text Files

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
__________________
~Ryan

rdove is offline   Reply With Quote
Old 10-10-2002, 06: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
Old 10-11-2002, 08:06 AM   #3 (permalink)
rdove
Masked Moderator
 
rdove's Avatar
 
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
rdove is on a distinguished road
Well..I'm not sure...That part of the project has sort of been put on hold for a while. Right now we are trying to get the page up and the transform part working. In the next couple of weeks I will be able to give you a better idea if thats what I'm looking for.
__________________
~Ryan

rdove is offline   Reply With Quote
Old 10-21-2002, 06:07 AM   #4 (permalink)
rdove
Masked Moderator
 
rdove's Avatar
 
Join Date: May 2002
Location: Indianapolis, IN
Posts: 260
rdove is on a distinguished road
I took a look at your code. And umm.... could you explain what each line does. I know nothing about perl (which is partly why this is hard for me) and your code is beyond the little bit I do know. I understnad if you don't want to explain it all, but is there a good site out there I can visit and read up on perl and text files?
__________________
~Ryan

rdove is offline   Reply With Quote
Old 10-21-2002, 06:06 PM   #5 (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: Re: Working with Text Files

No, I don't mind at all. Shame on me for throwing up code without explaining it :o
Code:
open (IN, "file") or die "Can't read file: $!\n";  # 1
while ( <IN>) {                                   # 2
    chomp;                                         # 3
    my @fields = split;                            # 4
    if ($fields[0] =~ /PATTERN/) {                 # 5
        # do something
    }
}
close(IN);                                         # 6
# 1 - Here I'm using the "open" function to attach the file handle IN to the external file "file". If it's unsuccessful, the script dies and gives an error message.
# 2 - I'm using the diamond operator (<>) with the file handle to read each line in the file. Used with "while" it means, "execute this block as long as there is a line to be read from IN". Each line gets set to the variable $_ in turn.
# 3 - Remove any trailing newline.
# 4 - Each line gets split up into a list (split uses the pattern /\s+/ to split by default). I regularly use files with tab-delimited fields, so this is convenient for me.
# 5 - This says, "execute this block if the first item in the list looks like this pattern".
# 6 - This just explicitly closes the file I was looking in.
Code:
my @lines = <IN>;                      # 1
my @stuff = grep { /PATTERN/ } @lines; # 2
print @stuff;                          # 3
You could use these lines in place of the while loop, if you wanted a list of entire lines for which a given expression is true (usually a pattern match).
# 1 - Read all lines from IN into a list, where the first element is the first line, and so on.
# 2 - This says, "if an element in the list @lines contains PATTERN, add it to the list @stuff". It's just quicker than writing a for loop with a conditional and all that.
# 3 - This just spits out whatever is in @stuff. Notice that the input wasn't chomped, and that the @stuff isn't surrounded with quotes. I'm assuming that the input file has one record on each line, and I don't want the print function to put extra spaces between the list elements.

I know there are some file access tutorials online, but I've found that they're generally limited in scope and can't compete with a good book at hand. Oh, and coderforums.net is another good forum to visit. There are plenty of people there that know a ton more about Perl than me
gwartheg is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[MySQL] blob vs text redhead PHP 2 03-29-2004 11:15 PM
working with files Miststlkr Standard C, C++ 2 10-13-2003 11:44 AM
Installing and using CMUgraphics library. Valmont Standard C, C++ 12 03-29-2003 08:39 AM


All times are GMT -8. The time now is 04:52 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting