PDA

View Full Version : Brain fried need help


SeventyTimes7
07-09-2008, 04:04 PM
Hello to all the gurus.
I am a returning novice to PERL/CGI programming and have, what I'm sure is a simple problem to solve.
I need to input a text file and format it in a different fashion.
Essentially, it is a text file of obituaries but needs to be formatted for an upload to a website.
Here is a snippet:
This how the file begins:
"Doris McJones, 82 SOMECITY — Doris McJones, of Somecity, ... " << this is all on one line.
What I need for it to be is:
Doris McJones, 82
SOMECITY — Doris McJOnes, of Somecity, ...

ASIDE: There can be many names in the file. The one I'm actually working with has 22 names.

So before I give myself a total headache and possible aneurism, I thought I would ask the pros.

Any and all help is GREATLY appreciated.
Thanks.
70x7 (AKA: Mike)
PS I have been able to write the large data input to a file (.tmp) on the system, so it can be slurped, formatted and then re-written as necessary, but unable to format the way I need it.

KevinADC
07-09-2008, 07:05 PM
basic concept:

$line = "Doris McJones, 82 SOMECITY — Doris McJones, of Somecity, ... ";
$line =~ s/^([\w ]+,\s*\d+)/$1\n/;
print $line;

SeventyTimes7
07-09-2008, 08:32 PM
Kevin, thanks for the input.
But it's not working as I would like. It is only printing out the 1st "record" of the data file.
As I mentioned, the text is written to a text file. Many lines and words. But what you have given me here it only prints out the 1st record. And doesn't put the first bit of info on a newline.
Here the routine I am using ...
sub format {
print "Content-type: text/html\n\n";
$TEXT_NAME="$tmpdatapath$form{'odate'}$EXT";
open (FILE, "<$TEXT_NAME")|| die "The File does not exist";
$line = <FILE>;
$line =~ s/^([\w ]+,\s*\d+)/$1\n/;
print <<__END__;
<html><body>
$line <br>
&lt;guestbook&gt;&lt;endobit&gt;<br><br>
</body>
</html>
__END__
}

(This is just ouput to webpage for now so I can "test" it. It will eventually re-write to a new text file for uploading.

I can supply the actual text of one if that would be better for you?

Again, I really appreciate your help.
TIA

Mike

SeventyTimes7
07-09-2008, 08:36 PM
Here is an actual section:

Harry E. Murray Jr., 75 MILFORD — Harry E. Murray Jr., passed peacefully on Friday, May 23, 2008, at Delaware Hospice Center, Milford, with his daughter at his side. He was 75. Harry was born on June 5, 1932, in Dagsboro, to the late Harry E.Murray Sr. and Maude McBunting. He is survived by one daughter Kellie Plummer and her husband Sammy; three grandsons, Todd, Michael, and Ryan Plummer; two great-grandsons, Mikie and Zachary Plummer; and one great-granddaughter, Jaida Plummer. Graveside services will be 2 p.m. Tuesday in the Delaware Veterans Memorial Cemetary, Millsboro.

What I need is to "slurp" the first bit, Harry E. Murray Jr., 75 and put it on a new line and have the remainder follow underneath, so it needs to look like this:

Harry E. Murray Jr., 75
MILFORD — Harry E. Murray Jr., passed peacefully on Friday, May 23, 2008, at Delaware Hospice Center, Milford, with his daughter at his side. He was 75. Harry was born on June 5, 1932, in Dagsboro, to the late Harry E.Murray Sr. and Maude McBunting. He is survived by one daughter Kellie Plummer and her husband Sammy; three grandsons, Todd, Michael, and Ryan Plummer; two great-grandsons, Mikie and Zachary Plummer; and one great-granddaughter, Jaida Plummer. Graveside services will be 2 p.m. Tuesday in the Delaware Veterans Memorial Cemetary, Millsboro.

oesxyl
07-09-2008, 08:59 PM
Hi,

if I don't miss something you want to format only the first line in a special way. If I'm not wrong the key to this must be comma, ",".
- split first line using /,/ as separator
- get slices and join back the items from the resulted array as you want to format and add comma where you need, :)

probably you will need aditional processing for some items. example is extracting age from second item
oops, first line is the only line for each record, :)

this is a improvisation, not tested based on "I work I don't think", :)

while(<>){
chomp;
if($_){
my @items = split(/[,.:;!]/,$_); # punctuation but not spaces
my ($name, $age, $address);
$name = shift @items;
$age = shift @items;
$age =~ s/^(\d+)\s+(.+?)\Q$name\E$/$1/;
$address = $2;
# .....
# output follow from here...
}
}
regards

KevinADC
07-09-2008, 09:58 PM
I can supply the actual text of one if that would be better for you?



Attach a copy of the file to a post.

FishMonger
07-09-2008, 10:43 PM
This is not the best way to code this, but it should get you closer to what you need.
print "Content-type: text/html\n\n";
print "<html><body>";

$TEXT_NAME="$tmpdatapath$form{'odate'}$EXT";
open (FILE, "<$TEXT_NAME")|| die "Can't open the file $!";

while ($line = <FILE> ) { # this is what you were missing
$line =~ s/^([\w ]+,\s*\d+)/$1\n/;
print "$line <br>&lt;guestbook&gt;&lt;endobit&gt;<br><br>";
}
print "</body></html>";