kieps has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to take a person's full name, "Firstname Middlename Lastname" and invert it to "Lastname, Firstname Middlename". I'm reading in a file and trying to write out to a sql script.
Here is the data:
Literature|30001f3d|John M. DoeThis is what I'm trying to get:
INSERT INTO Books Title,FileID,author,authInv) VALUES ('Literature','30001f3d','John M. Doe','Doe, John M.');
I'm new to perl so I don't know how to write a script to invert it. I've seen a code before where you take the full name, start the counter at the end, go until you hit a space and reformat the field to add the comma but I just can't make it work. Any help would be appreciated. Thanks.
Here is the code:
Edited 2005-04-08 by Ovidprint "What is the directory where we'll be working: "; $directory = <STDIN>; chomp ($directory); print "What is the file name, with a file extension: "; $file = <STDIN>; chomp ($file); $originalfile = "$directory/$file"; open(FIRST, "<$originalfile" ) || die "I cannot open $originalfile. $! +"; @contents = <FIRST>; close(FIRST); $new = "$directory/SQL-Insert-Titles.sql"; unlink($new); open(SECOND, ">$new") || die "I cannot open $new. $!"; foreach $line (@contents) { @splitline = split /\|/, $line; $title = $splitline[0]; $file = $splitline[1]; $author = $splitline[2]; $authinv = $splitline[3]; chomp($title); chomp($file); print SECOND "INSERT INTO Books Title,FileID,author,authInv) VALUES ('$title','$file','$author','$authinv')\;\n"; } close(SECOND); print "\n\tDone."; |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inverting full names
by ikegami (Patriarch) on Apr 08, 2005 at 20:30 UTC | |
|
Re: Inverting full names
by Grygonos (Chaplain) on Apr 08, 2005 at 20:53 UTC | |
|
Re: Inverting full names
by doom (Deacon) on Apr 08, 2005 at 23:59 UTC | |
|
Re: Inverting full names
by Cody Pendant (Prior) on Apr 09, 2005 at 08:53 UTC | |
by jhourcle (Prior) on Apr 09, 2005 at 12:16 UTC |