With the solutions presented, I used rindex since what about the title: 'Perl by example by New Author' . Doing a split and you will not have what you expected. Here is a shot:
while ( <DATA> ) { chomp; next if ( /^\s*$/ ); my $MyPtr = rindex($_," by "); if ( $MyPtr < 0 ) { printf "Expecting a title followed \' by \' Author. Data:\n"; printf "<%-s>\n", $_; next; } my $MyTitle = substr($_,0,$MyPtr); my $MyAuthor = substr($_,($MyPtr+4)); printf "%-s - %-s\n", $MyAuthor, $MyTitle; } __DATA__ 1984 by George Orwell A BEND IN THE RIVER by V.S. Naipaul A CLOCKWORK ORANGE by Anthony Burgess A DANCE TO THE MUSIC OF TIME (series) by Anthony Powell A FAREWELL TO ARMS by Ernest Hemingway A HANDFUL OF DUST by Evelyn Waugh Output: George Orwell - 1984 V.S. Naipaul - A BEND IN THE RIVER Anthony Burgess - A CLOCKWORK ORANGE Anthony Powell - A DANCE TO THE MUSIC OF TIME (series) Ernest Hemingway - A FAREWELL TO ARMS Evelyn Waugh - A HANDFUL OF DUST New Author - Perl by example Wags ;) -----Original Message----- From: Hanson, Robert [mailto:[EMAIL PROTECTED]] Sent: Wednesday, June 05, 2002 06:57 To: 'Denham Eva'; BeginnersPerl (E-mail) Subject: RE: Regex Problem - please help Here is my solution, others will differ... # always print $! on error so you can see the cause open( INFILE,"books.txt" ) || die "Cann't Open: $!"; while( <INFILE> ) { chomp; # remove the newline next unless ($_); # skip blank lines # split the line by the seperator my @parts= split(/ by /, $_); # die unless exactly 2 parts are found die "Can't split properly" unless ( @parts == 2 ); print "$author - $book\n"; } close INFILE; Rob -----Original Message----- From: Denham Eva [mailto:[EMAIL PROTECTED]] Sent: Wednesday, June 05, 2002 9:42 AM To: BeginnersPerl (E-mail) Subject: Regex Problem - please help Hello Listers, I am struggling to get this right. Beginner in perl, so please forgive ignorance, but the regular expressions are confusing. I am trying to read in a file, with content as follows ---snip--- 1984 by George Orwell A BEND IN THE RIVER by V.S. Naipaul A CLOCKWORK ORANGE by Anthony Burgess A DANCE TO THE MUSIC OF TIME (series) by Anthony Powell A FAREWELL TO ARMS by Ernest Hemingway A HANDFUL OF DUST by Evelyn Waugh ---end snip--- The idea is to list by Author ie. George Orwell - 1984 V.S. Naipaul - A BEND IN THE RIVER Anthony Burgess - A CLOCKWORK ORANGE Anthony Powell - A DANCE TO THE MUSIC OF TIME (series) Ernest Hemingway - A FAREWELL TO ARMS Evelyn Waugh - A HANDFUL OF DUST OK my code thus far is:- --- Spin Code --- open(INFILE,"books.txt")||die "Cann't Open"; while(<INFILE>) { $_ =~ m/[A-Fa-z0-9]/ || die "Sorry Don't Work"; # Here regex is for everything # But I would like to chop it up to divide Authors from Books # Then to add them together with:- print "$author - $Book" -:for instance # In the end to write the output to a seperate file. $index = $_; print $index; } --- Close Code --- Now this produces a whole string, which I know, unfortunately I am not to good with the regex part. Any help will be much appreciated. Denham Eva Oracle DBA In UNIX Land On a quiet Night, you can hear the Windows machines reboot. ############################################################################ ######### This e-mail message has been scanned for Viruses and Content and cleared by MailMarshal For more information please visit www.marshalsoftware.com ############################################################################ ######### -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]