Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: C vs perl

by Ovid (Cardinal)
on Apr 28, 2002 at 06:16 UTC ( #162623=note: print w/ replies, xml ) Need Help??


in reply to C vs perl

Well, I can't comment on the C code, but I think you can make the Perl a bit clearer (or at least more correct). The following assumes that you want each <p>...</p> section on a new line. If not, the final join should be on an empty string.

sub MakePtag{ my $fixme = shift; # take in our parameters return join "\r\n", # join with newlines map { "<p>$_</p>" } # wrap each line in <p></p> tags grep { /\S/ } # must have at least one non-whitespace + character split "\r\n", $fixme; # break apart on the newlines }

Of course, I'd be shot for suggesting:

sub MakePtag { join "\r\n", map {"<p>$_</p>"} grep {/\S/} split "\r\n" +, $_[0] } # :)

If you stick with your solution, you'll want to chomp $fixme to avoid wasted tags on the end. Oh, and you had the slash on the trailing paragraph tag backwards :)

sub MakePtag{ chomp(my $fixme = shift;) # take in our parameters $fixme=~s|(\r\n)|</p><p>|g; # replace all \r\n with <\p><p> $fixme = "<p>$fixme</p>"; # Add beginning and ending tags return $fixme; }

Interesting question: what hoops would you have to just through with C to duplicate the grep functionality that I tossed in?

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


Comment on Re: C vs perl
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2015-07-03 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (55 votes), past polls