Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Unwanted line breaks-- why?

by jeffrgsf (Novice)
on Oct 25, 2012 at 20:03 UTC ( #1000929=perlquestion: print w/ replies, xml ) Need Help??
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"; }

Comment on Unwanted line breaks-- why?
Download Code
Re: Unwanted line breaks-- why?
by VinsWorldcom (Priest) on Oct 25, 2012 at 20:16 UTC
    foreach $line (@works) { chomp $line; # ADD THIS LINE
      ....and there you go! Thanks Master VinsWorldcom!

      "When I first met you, I was but a learner..." --Darth Vader
Re: Unwanted line breaks-- why?
by Kenosis (Priest) on Oct 25, 2012 at 21:48 UTC

    Another option is to:

    chomp( my @works = <FILEY> );
Re: Unwanted line breaks-- why?
by pvaldes (Chaplain) on Oct 26, 2012 at 00:14 UTC

    If you want a database, maybe you should take a different focus and create a more flexible structure from the beginning.

    %classic = ( TheMagicFlute1791Mozart => { title => "The Magic Flute", type => "opera", author => "Mozart", year => "1791", #... space for lots and lots of optional fields... }, Armida1783Haydn=> { title => "Armida", type => "Opera", author => "Haydn", year => "1783", characters => ["Armida","Rinaldo","Zelmira",...], # libretto => FORGET, ADD LATER, }, );

    See hashes of hashes, hashes of arrays and perldoc perlreftut

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1000929]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (14)
As of 2014-10-01 12:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (10 votes), past polls