jackassplus wrote:
I am trying to remove single newlines but not double newlines.

for instance say I have the following:
Name:\nMy Name\n\nAddress:\n123 Anywhere St\n\nAbout Me:\nSome text
that\nhas some newlines that\nI want to be rid of\n\nThe End\n

I want to get rid of all of the newlines between Me:\n and the next
occurrence of \n\n. and replace them with spaces so it says:
Name:\nMy Name\n\nAddress:\n123 Anywhere St\n\nAbout Me:\nSome text
that has some newlines that I want to be rid of\n\nThe End\n

I'm completely at a loss on how to pull this off...

sub fixer($){

You really shouldn't use prototypes.
http://groups.google.com/group/comp.lang.perl.modules/msg/84484de5eb01085b


    my $data = $_[0];
    $data =~ s/\\n{1}/ /g; #this is too greedy

The string "\\n" will not match a newline, it will match the two characters '\' and 'n'. The comment says "too greedy" but there is no greediness involved. The pattern matches exactly one '\' character and exactly one 'n' character. You would need a '?', '*', '+' or '{n,m}' modifier to invoke greediness.


    print $data;

Why the side effect of printing $data and returning the value that print() returns instead of just returning $data?


}


This may do what you require:

s/(?<=.)\n(?=.)/ /g




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to