Connecting Tech Pros Worldwide Forums | Help | Site Map

plain-text file parsing

Russell Klopfer
Guest
 
Posts: n/a
#1: Jul 19 '05
Hello. I would like to know how I can parse a plain-text file. All I
want to do is be able to sequentially extract each word from a
document. Similar to the StringTokenizer in Java. Is there a module
for this? or an easy way to do it with regular expressions?

Thanks!

Roger Nordqvist
Guest
 
Posts: n/a
#2: Jul 19 '05

re: plain-text file parsing


Russell Klopfer wrote:
[color=blue]
> Hello. I would like to know how I can parse a plain-text file. All I
> want to do is be able to sequentially extract each word from a
> document. Similar to the StringTokenizer in Java. Is there a module
> for this? or an easy way to do it with regular expressions?
>
> Thanks![/color]

Dont really know what you want to do with the textfile.

A usual way to work with file:

open FILE, "/home/barry/text.txt" or
die "Cant open file";

while( $line = <FILE>)
{
... do something with $line ...
}

close FILE;


This parses the textfile line by line

/Roger
Jürgen Exner
Guest
 
Posts: n/a
#3: Jul 19 '05

re: plain-text file parsing


Roger Nordqvist wrote:[color=blue]
> Russell Klopfer wrote:[color=green]
>> Hello. I would like to know how I can parse a plain-text file. All I
>> want to do is be able to sequentially extract each word from a
>> document. Similar to the StringTokenizer in Java. Is there a module
>> for this? or an easy way to do it with regular expressions?[/color]
>
> Dont really know what you want to do with the textfile.
> A usual way to work with file:
>
> open FILE, "/home/barry/text.txt" or
> die "Cant open file";
> while( $line = <FILE>)
> {
> ... do something with $line ...
> }
> close FILE;[/color]

Actually the idiomatic Perl way would be
while(<FILE>) {
... do something with $_ ...
}

jue


Closed Thread