For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Reading text lines









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reading text lines
keller789

2006-11-06, 6:18 am

I have a text file of book information with titles, authors and the first chapter of each book displayed in the following way:

.T
title of book
.A
author of book
.C
once upon a time...

I want to be able to search through the file until I find a particular field (say .T) and then read off the following line. The only way I could think to do this was to convert the file into a single line by replacing the returns with spaces, and then to use a regular expression to pick out each piece of information.

The code looks something like -


while ($text = <INFILE> ) {

$text =~ s/\n/\t/g; #replace newline with space

if ($text =~ /(\.T)(.+)(\.A)/){ # find .T followed by any string followed by .A
$title = $2; # assign the middle string to $title
}
}


But $title is not returning anything, so I'm assuming that there is some problem with my RegEx.

If anybody could tell me what is wrong with my expression, or preferably suggest an easier way of going about the problem, I'd be very grateful.

Thanks, Sarah
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com