Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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"; }

In reply to Unwanted line breaks-- why? by jeffrgsf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-16 21:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found