Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

--why the code still not working

The short answer to your question is that you are trying to do way too much in a single regular expression.

Try breaking down the problem into smaller steps. First get a program that does what you want with the lines beginning with AU. When you get that right, then and only then expand the program to also replace ids.

Even doing the right thing with lines that begin with AU can be broken down into single steps. You don't need to select the lines beginning with 'AU' and do the substitutions and insert "Written by" all in one go. Instead break it down into steps:

  1. Use if ($line =~ /^AU:/) {...} to select the lines beginning with 'AU' for special processing.
  2. Find the place to insert "Written by". Do this as a single step on its lonesome.
  3. Capitalize each word using a simple regular expression, something like this: s/\s(\w+)/ \u$1/g

Here is a part of your problem broken down into smaller steps. You might find substr especially helpful. It can be used on the left side to replace parts of a string.

my $iLength = length('AU: Written by:'); # read lines one at a line instead of slurping into an array # this is more memory friendly while (my $line = <INPUT>) { # single out lines beginning with 'AU' for special processing if ($line =~ /^AU:/) { # insert "Written by" # Note: substr on the left means # replace the zero-length string at position 3 # in other words: insert something at position 3 substr($line, 3,0) = ' Written by:'; # capitalize the first letter of every word # Note: substr on the left means # make substitutions in the part of the string after # "AU: written by" substr($line, $iLength) =~ s/\s(\w+)/ \u$1/g; } print $line; }

In reply to Re: Display the extracted ID in ID: tag before AU: tag and problem in capitalizing first letter in each word by ELISHEVA
in thread Display the extracted ID in ID: tag before AU: tag and problem in capitalizing first letter in each word by allison

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 avoiding work at the Monastery: (3)
As of 2024-04-25 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found