http://www.perlmonks.org?node_id=1000929

jeffrgsf has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks,

I am new to programming and this site is helping me A LOT. I'm so close to finishing this script to chop up strings of classical music work titles into title, key, instruments, etc. but for some mysterious reason perl adds a line break I don't want. Why why why??

Script reads a big text file and generates a new file-- ideally with one-to-one line correspondence. These extra line breaks are RUINING MY LIFE! (Ok, not really, but they're a bummer.)

Examples of what I mean and the code I wrote are below.

Sorry this is such a noob question. Thanks for any help!!!


Example input lines:
----------------
Concertino, For Clarinet & Strings (Or Piano) (Based On Tartini Sonata Themes)
Concertino, For Clarinet, Piano, Violin & Cello, Op. 73
Concertino, For Clarinet, Violin & Piano, "Fritz Zorn"
Concertino, For Contrabass & Orchestra
Concertino, For Corno, Bassoon & Orchestra In E Flat


Example output lines (not what I want-- should be each on one line!)
----------------
Concertino, For Clarinet & Strings (Or Piano) (Based On Tartini Sonata Themes) -;;- Concertino Based On Tartini Sonata Themes
-;;- -;;- Clarinet & Strings -;;- -;;- -;;- Or Piano -;;-
Concertino, For Clarinet, Piano, Violin & Cello, Op. 73 -;;- Concertino Piano Violin & Cello
-;;- -;;- Clarinet -;;- Op. 73 -;;- -;;- -;;-
Concertino, For Clarinet, Violin & Piano, "Fritz Zorn" -;;- Concertino Violin & Piano
-;;- -;;- Clarinet -;;- -;;- "Fritz Zorn" -;;- -;;-
Concertino, For Contrabass & Orchestra -;;- Concertino -;;- -;;- Contrabass & Orchestra
-;;- -;;- -;;- -;;-
Concertino, For Corno, Bassoon & Orchestra In E Flat -;;- Concertino Bassoon & Orchestra -;;- E Flat
-;;- Corno -;;- -;;- -;;- -;;-


Eventually I need to run this on a file over 170,000 lines, but I'm just doing short bits until I get the bugs worked out. I know it would be better not to be posting all this code, but I can't figure out where the problem is.
#!/usr/bin/perl use warnings; use strict; use IO::File; open (FILEY, "tinyWorks.txt") || die "Couldna open it $!\n"; my @works = <FILEY>; my $line = ''; foreach $line (@works) { my $work = $line; my $unmodified_work = $work; my $nickname = ''; my $opus = ''; my $key = ''; my $parenth = ''; my $instruments = ''; my @all_parenth = (); #REMOVE NICKNAME (Anything in quotes) if ( $work =~ /(".+")/ ) { $nickname = $1; $nickname =~ s/[()]//g; #remove extra parentheses left over in + extracted string -- WHY ARE THEY EVEN THERE??? $work =~ s/$nickname//g; #remove nickname $nickname =~ s/^\s+//; #remove leading spaces, if any } #REMOVE OPUS ("Op..." or "WoO) if ( $work =~ /(Op.? \S+)/ || $work =~ /(WoO.? \S+)/ ) { $opus = $1; $opus =~ s/[()]//g; #remove extra parentheses left over in ext +racted string -- WHY ARE THEY EVEN THERE??? $work =~ s/$opus//g; #remove opus $opus =~ s/,//g; $opus =~ s/^\s+//; #remove leading spaces, if any } #REMOVE KEY ("In...") if ( $work =~ /(In A [^,^\(]+)/ || $work =~ /(In B [^,^\(]+)/ || $work =~ /(In C [^,^\(]+)/ || $work =~ /(In D [^,^\(]+)/ || $work =~ /(In E [^,^\(]+)/ || $work =~ /(In F [^,^\(]+)/ || $work =~ /(In G [^,^\(]+)/ ) { $key = $1; $key =~ s/[()]//g; #remove extra parentheses left over in ext +racted string -- WHY ARE THEY EVEN THERE??? $work =~ s/$key//g; #remove nickname $key =~ s/^\s+//; #remove leading spaces, if any $key = substr($key, 3, 100); #remove the substring "In " from +beginning } #REMOVE INSTRUMENTS ("For...") if ( $work =~ /(For[^,^\(]+)/ ) { $instruments = $1; $instruments =~ s/[()]//g; #remove extra parentheses left over + in extracted string -- WHY ARE THEY EVEN THERE??? $work =~ s/$instruments//g; #remove nickname $instruments =~ s/^\s+//; #remove leading spaces, if any $instruments = substr($instruments, 4, 100); #remove the subst +ring "For " from beginning } #REMOVE PARENTHESES (any remaining) while ( $work =~ /(\([^)]*\))/ ) { $parenth = $1; $work =~ s/$parenth//g; #remove anything in parentheses $parenth = substr( $parenth, 1, (length($parenth) - 2) ); #rem +ove parentheses themselves from extracted substring $parenth =~ s/^\s+//; #remove leading spaces from extracted pa +renth string, if any $work =~ s/[()]//g; #remove extra parentheses left over in wor +k -- WHY ARE THEY EVEN THERE??? push (@all_parenth, $parenth); } #CLEAN UP REMAINING STRING $work =~ s/[()]//g; #remove extra parentheses left over in work -- + WHY ARE THEY EVEN THERE??? $work =~ s/,//g; #remove commas $work =~ s/^\s+//; #remove leading spaces, if any $unmodified_work =~ s/\n//; print $unmodified_work, " -;;- ", $work, " -;;- ", $key, " -;;- ", $instruments, " -;;- ", $opus, " -;;- ", $nickname, " -;;- ", join("& ", @all_parenth), " -;;- ", "\n"; }