Re: equivalent of chomp in perl



Jordan Abel <random@xxxxxxxxxxxxx> writes:
[...]
Anyway, in answer to the question

void
chomp(char *x) {
char *p = strrchr(x,'\n');
if(p) *p = 0;
}

Not quite.

Let's look at the definition from the Camel Book (the canonical book
on Perl, comparable to K&R). This is arguably a little off-topic, but
it's a good example of the pitfalls of emulating features of one
language in another language.

chomp VARIABLE
chomp LIST
chomp

This function (normally) deletes a trailing newline from the end
of a string contained in a variable. This is a slightly safer
version of chop (described next) in that it has no effect upon a
string that doesn't end in a newline. More specifically, it
deletes the terminating string corresponding to the current value
of $/, and not just any last character.

Unlike chop, chomp returns the number of characters deleted. If $/
is "" (in paragraph mode), chomp removes all trailing newlines
from the selected string (or strings, if chomping a LIST). You
cannot chomp a literal, only a variable.

Some of the features of chomp aren't applicable to a C version: it can
be applied to either a list or a single variable, and the argument
defaults to $_ (if you're not familiar with Perl, don't worry about
what that means). And C has no equivalent to $/ (which in Perl lets
you set the input record separator to something other than the default
"\n").

So a reasonable C chomp() would operate on a single string, would
remove the last character if and only if it's a '\n', and would return
1 if it removed a character and 0 if it didn't.

Jordan, your version clobbers the last '\n' in the string, even if
it's not at the end of the string.

Here's my attempt:

int chomp(char *s)
{
size_t len = strlen(s);
if (len == 0) {
return 0;
}
else if (s[len-1] == '\n') {
s[len-1] = '\0';
return 1;
}
else {
return 0;
}
}

Another difference from Perl is that C arrays don't re-size
themselves. In Perl, strings are first-class objects. In C, a string
is data format, not a data type; if you shorten a string like this,
the remainder of the string is still there in the array. (A C dynamic
string library would probably deal with this.)

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: equivalent of chomp in perl
    ... chomp VARIABLE ... string that doesn't end in a newline. ... and not just any last character. ... char *s; ...
    (comp.lang.c)
  • Re: Interesting PERL anamoly - confirmation and/or explanations welcomed
    ... to find out if this is just a problem with the ActiveState PERL ... string matching against the first part of each line and based on some ... print OUTFILE; ... chomp; chomp; chomp; ...
    (comp.lang.perl.misc)
  • Re: Q: re Inline and Benchmark
    ... What on earth is perl on about there ?? ... Ditto as above. ... On a couple of occasions it returned a string ... >NULL character in the string, so it probably pays to check that you get ...
    (comp.lang.perl.misc)
  • Re: equivalent of chomp in perl
    ... on Perl, comparable to K&R). ... chomp VARIABLE ... string that doesn't end in a newline. ... char *s; ...
    (comp.lang.c)
  • Chopping off first&last character in a string
    ... is there an equal but opposite function to chop in Perl? ... I'd like to remove the first and last character of a string and am looking ... If you are not the intended recipient of ...
    (perl.beginners)