Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

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

I'm preparing a 30 minute presentation comparing C to Perl for my club. The audience fellow undergraduates with some exposure to C and no exposure to Perl. This is not official homework for a grade. I plan to include a pointer to this thread in my presentation.

Below are two functions, one in Perl, one in C. Both are supposed to replace Microsoft \n\r with hmtl  <p></p> tags. Both functions were kludged up by banging randomly on the problem until I more or less got the right answer. For what it is worth, I wrote the C function two years ago because I was too lazy to tackle perl.

I would probably benefit from suggestions like "just open the file in text mode.". However, for my immediatepurpose I'm most interested in knowing:
  1. Do these functions do approximately the same thing?
  2. Is there an cleaner way to do this in C?

C

extern char * danCGIReplacedCRLF(char * szFixMe){ int i,j,iLen=0,iNewLen=0; int iCRLFcount=0; char* szResult=NULL; iLen=strlen(szFixMe); for (i=0;i<=iLen;i++){ if (szFixMe[i]==(int) '\n' || szFixMe[i]==(int) '\r') iCRLFcount++; } // need space for first <p> +NULL+ text // + replace each CR &LF + last " <\p>" iNewLen=3+1+strlen(szFixMe)+(iCRLFcount*8)+5; szResult = malloc(iNewLen); if (szResult){ szResult[0]='<'; szResult[1]='p'; szResult[2]='>'; szResult[3]='\0'; i=0; j=3; while (i<=iLen) if (szFixMe[i]==(int) '\n' || szFixMe[i]==(int) '\r'){ strcat(szResult," </p><p>"); j+=8; // deal with Newline CR pairs if(szFixMe[i]==(int) '\n' && szFixMe[i+1]==(int) '\r' || szFixMe[i]==(int) '\r' || szFixMe[i+1]==(int) ' +\n') i+=2; else i++; }else{ szResult[j]=szFixMe[i]; j++; szResult[j]='\0'; //dest string in strcat needs termin +ating \0 i++; } //outer loop & if statement //end of new document needs </p> to match at start strcat(szResult," </p>"); } // if(szResult !=NULL ) return szResult; }

perl

sub MakePtag{ my ($fixme)=@_; # take in our parameters $fixme='<p>'.$fixme; # Prepend a <p> tag to our string $fixme=~s|(\r\n)|<\\p><p>|g; # replace all \r\n with <\p><p> $fixme.='<\p>'; # Append closing <\p> tag return $fixme; }


email: mandog

In reply to C vs perl by mandog

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: (4)
As of 2024-04-25 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found