Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: C vs. Perl

by sauoq (Abbot)
on Oct 22, 2003 at 21:04 UTC ( [id://301366]=note: print w/replies, xml ) Need Help??


in reply to Re: C vs. Perl
in thread C vs. Perl

So can someone explain what's so intractable about this problem in C?

Read the OP again. He never said it couldn't be done. He said it was easier in Perl. And he's right. It seems that gmax and others thought they were disagreeing with the OP but were agreeing all along. Still, as dragonchild said, the post was really just advocating Perl, which is hardly necessary around here.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: C vs. Perl
by Cody Pendant (Prior) on Oct 22, 2003 at 21:36 UTC
    Yes, fair enough, but I withdraw my "intractable" and rephrase -- what makes this problem so much harder in C?

    It's just you all seem to be skipping over that part and saying "yes, but" whereas I'm not even at "yes".

    Is it because in C you can't have an array that just gets bigger and bigger to handle all the data that you shove into it, it has to have a pre-declared size, something like that?



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print

      Okay. Here's a somewhat complete though simplistic implementation of the program written in C. It is more complete, in that it includes the code for prompting the user and retrieving the input, and makes some attempt at error checking (not enough), that was completely absent from the perl version above. It also cheats by using a linear search rather than hashing the lookup.

      #include <stdio.h> #include <stdlib.h> #include <sys\stat.h> #include <string.h> #include <fcntl.h> #include<io.h> #define FILE "data" #define LINESIZE 80 int main( ) { struct stat statbuf; char *linebuf, *p, *found; int fh, price; if( ! stat( FILE, &statbuf ) && ( p = calloc( statbuf.st_size, sizeof( char ) ) ) && ( fh = open( FILE, O_RDONLY|O_BINARY, S_IREAD ) ) && ( read( fh, p, statbuf.st_size ) > 0 ) && ! close( fh ) ) { printf( "%s", "Fruit? " ); linebuf = calloc( LINESIZE, sizeof( char ) ); fgets( linebuf, LINESIZE, stdin ); *( linebuf + strlen( linebuf ) -1 ) = 0; if( (found = strstr( p, linebuf )) ) { sscanf( found + strlen( linebuf ), "%i", &price ); printf( "%s costs %d", linebuf, price ); } else { printf( "No price for %s available\n", linebuf ); } free( p ); free( linebuf ); } else { printf( "Error: %d", errno ); } return (0); }

      This is about as simple as it is possible to do write something roughly equivalent in C, whereas I've no doubt that this coudl be done as a perl one-liner with a little effort.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Hooray!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-25 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found