473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

equivalent of chomp in perl

Hi,
Is there an equivalent to the perl command chomp in C? And if there is
no exact equivalent command, how would I go about removing the "\n" at
the end of a stdin?

Thank you,
Natalie

Nov 3 '06 #1
35 19390
"lnatz" <nm**********@gmail.comwrites:
Is there an equivalent to the perl command chomp in C?
No. (chomp removes a '\n' character from the end of a string.)
And if there is no exact equivalent command, how would I go about
removing the "\n" at the end of a stdin?
I presume that by "a stdin", you mean "a string obtained from stdin".

Search for the '\n' character and replace it with '\0'. Be prepared
to decide what to do if the string doesn't contain a '\n' character,
or if it contains one but not at the end.

Note that C strings are not like Perl strings. They don't
automatically re-size themselves. A C string isn't a data type; it's
a data *format*, a sequence of character terminated by '\0'. A string
is typically held in an array of char; changing a trailing '\n' to
'\0' will change the length of the string but not the size of the
array.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 3 '06 #2
lnatz wrote:
>
Is there an equivalent to the perl command chomp in C? And if
there is no exact equivalent command, how would I go about
removing the "\n" at the end of a stdin?
int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 3 '06 #3
CBFalconer <cb********@yahoo.comwrites:
lnatz wrote:
>Is there an equivalent to the perl command chomp in C? And if
there is no exact equivalent command, how would I go about
removing the "\n" at the end of a stdin?

int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}
That's a useful function, but it's doesn't bear any particular
resemblance to Perl's chomp function.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 3 '06 #4
lnatz wrote:
Is there an equivalent to the perl command chomp in C?

Perl's chomp() is actually a fairly complex function, especially in a
list context.

Do you just want to remove a newline from a string?

void chomp(const char *s)
{
char *p;
while (NULL != s && NULL != (p = strrchr(s, '\n'))){
*p = '\0';
}
} /* chomp */
Nov 3 '06 #5
james of tucson <jmcgill@[go_ahead_and_spam_me].arizona.eduwrites:
lnatz wrote:
>Is there an equivalent to the perl command chomp in C?


Perl's chomp() is actually a fairly complex function, especially in a
list context.

Do you just want to remove a newline from a string?

void chomp(const char *s)
{
char *p;
while (NULL != s && NULL != (p = strrchr(s, '\n'))){
*p = '\0';
}
} /* chomp */
What if '\n' is in the middle of string? (Moreover, it's not nice
telling you won't modify the argument and then modifying it.)

#v+
#include <string.h>

int chomp(char *str) {
if (!str || !*str) return 0;
while (str[1]) ++str;
if (*str!='\n') return 0;
*str = 0;
return '\n';
}
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Nov 3 '06 #6
Michal Nazarewicz wrote:
What if '\n' is in the middle of string?
Then don't touch it!
Moreover, it's not nice
telling you won't modify the argument and then modifying it.)
const char * means that the pointer is fixed, not that it points to some
immutable type.
Nov 3 '06 #7
2006-11-03 <_h********************@newsfe10.phx>,
james of tucson wrote:
Michal Nazarewicz wrote:
>What if '\n' is in the middle of string?

Then don't touch it!
>Moreover, it's not nice
telling you won't modify the argument and then modifying it.)

const char * means that the pointer is fixed, not that it points to some
immutable type.
No, that would be char * const. which hardly matters anyway in function
arguments.
Nov 3 '06 #8
Jordan Abel wrote:
2006-11-03 <_h********************@newsfe10.phx>,
james of tucson wrote:
>Michal Nazarewicz wrote:
>>What if '\n' is in the middle of string?
Then don't touch it!
>>Moreover, it's not nice
telling you won't modify the argument and then modifying it.)
const char * means that the pointer is fixed, not that it points to some
immutable type.

No, that would be char * const. which hardly matters anyway in function
arguments.
Seriously, am I wrong about this? I know that "const char[]" states
that the array contents will not be altered (K&R2, p40), but that's not
the situation here. Here we are talking about declaring a pointer to a
char as const, not that it is a pointer to a const array of chars.
Nov 3 '06 #9
2006-11-03 <Gy****************@newsfe15.phx>,
james of tucson wrote:
Jordan Abel wrote:
>2006-11-03 <_h********************@newsfe10.phx>,
james of tucson wrote:
>>Michal Nazarewicz wrote:

What if '\n' is in the middle of string?
Then don't touch it!

Moreover, it's not nice
telling you won't modify the argument and then modifying it.)
const char * means that the pointer is fixed, not that it points to some
immutable type.

No, that would be char * const. which hardly matters anyway in function
arguments.

Seriously, am I wrong about this? I know that "const char[]" states
that the array contents will not be altered (K&R2, p40), but that's not
the situation here. Here we are talking about declaring a pointer to a
char as const, not that it is a pointer to a const array of chars.
If you want the _pointer_ rather than what is pointed _at_ to be
constant, "const" goes after the * token.

char * const p;
Nov 3 '06 #10
Michal Nazarewicz wrote:
>What if '\n' is in the middle of string?
james of tucson <jmcgill@[go_ahead_and_spam_me].arizona.eduwrites:
Then don't touch it!
Your code does touch it.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Nov 3 '06 #11
Michal Nazarewicz wrote:
>Michal Nazarewicz wrote:
>>What if '\n' is in the middle of string?

james of tucson <jmcgill@[go_ahead_and_spam_me].arizona.eduwrites:
>Then don't touch it!

Your code does touch it.
So it does. Sorry, I don't pay much attention when helping others with
their homework, since it's their grade not mine :-)

Sincere apologies.
Nov 3 '06 #12
On Fri, 03 Nov 2006 09:58:14 -0700, james of tucson wrote:
Jordan Abel wrote:
>2006-11-03 <_h********************@newsfe10.phx>,
james of tucson wrote:
>>const char * means that the pointer is fixed, not that it points to some
immutable type.

No, that would be char * const. which hardly matters anyway in function
arguments.

Seriously, am I wrong about this? I know that "const char[]" states
that the array contents will not be altered (K&R2, p40), but that's not
the situation here. Here we are talking about declaring a pointer to a
char as const, not that it is a pointer to a const array of chars.
Yes.

% cdecl explain 'const char *foo'
declare foo as pointer to const char

--
James Antill -- ja***@and.org
http://www.and.org/and-httpd/ -- $2,000 security guarantee
http://www.and.org/vstr/

Nov 3 '06 #13
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>lnatz wrote:
>>Is there an equivalent to the perl command chomp in C? And if
there is no exact equivalent command, how would I go about
removing the "\n" at the end of a stdin?

int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}

That's a useful function, but it's doesn't bear any particular
resemblance to Perl's chomp function.
Probably so, but I suspect it is the answer to the OPs real
question, which was not well defined.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 3 '06 #14
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>CBFalconer <cb********@yahoo.comwrites:
>>lnatz wrote:

Is there an equivalent to the perl command chomp in C? And if
there is no exact equivalent command, how would I go about
removing the "\n" at the end of a stdin?

int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}

That's a useful function, but it's doesn't bear any particular
resemblance to Perl's chomp function.

Probably so, but I suspect it is the answer to the OPs real
question, which was not well defined.
If you happen to know what Perl's chomp function does (which is
admittedly off-topic), the question is perfectly well defined, though
it could have been worded a bit better. The OP wants to remove a
trailing '\n' from a string (for example, from a string representing a
line read by fgets(). There's no indication that he wanted to discard
a line of input.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 3 '06 #15
2006-11-03 <45***************@yahoo.com>,
CBFalconer wrote:
Keith Thompson wrote:
>CBFalconer <cb********@yahoo.comwrites:
>>lnatz wrote:

Is there an equivalent to the perl command chomp in C? And if
there is no exact equivalent command, how would I go about
removing the "\n" at the end of a stdin?

int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}

That's a useful function, but it's doesn't bear any particular
resemblance to Perl's chomp function.

Probably so, but I suspect it is the answer to the OPs real
question, which was not well defined.
Are you sure? chomp is used for a specific purpose, which is removing
the newline from a whole line read from input. fgets() also leaves the
newline on the input line. The function above is more useful for people
who think they want to fflush(stdin) [after, say, a scanf] - but the OP
didn't say anything like that.
Nov 3 '06 #16
2006-11-03 <ln************@nuthaus.mib.org>,
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>Keith Thompson wrote:
>>CBFalconer <cb********@yahoo.comwrites:
lnatz wrote:

Is there an equivalent to the perl command chomp in C? And if
there is no exact equivalent command, how would I go about
removing the "\n" at the end of a stdin?

int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}

That's a useful function, but it's doesn't bear any particular
resemblance to Perl's chomp function.

Probably so, but I suspect it is the answer to the OPs real
question, which was not well defined.

If you happen to know what Perl's chomp function does (which is
admittedly off-topic), the question is perfectly well defined, though
it could have been worded a bit better. The OP wants to remove a
trailing '\n' from a string (for example, from a string representing a
line read by fgets(). There's no indication that he wanted to discard
a line of input.
Anyway, in answer to the question

void
chomp(char *x) {
char *p = strrchr(x,'\n');
if(p) *p = 0;
}
Nov 3 '06 #17
In article <sl*******************@rlaptop.random.yi.org>,
Jordan Abel <ra*******@gmail.comwrote:

[with respect to perl chomp]
>Are you sure? chomp is used for a specific purpose, which is removing
the newline from a whole line read from input.
That would be chop, not chomp. chomp removes the newline only
if the newline is there. This is useful if you cannot guarantee
that the newline will be there (such as at end of file, or when
dealing with serial I/O or with network programming, as line
or message boundaries are not -certain- to be preserved for those).

It is also useful for robust modular programming where you don't want or
need the newline but you also don't want to force callers of the
routine to have to have removed it as a precondition to calling
your routine.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Nov 3 '06 #18
In article <sl*******************@rlaptop.random.yi.org>,
Jordan Abel <ra*******@gmail.comwrote:
>>>>lnatz wrote:
>>>>>Is there an equivalent to the perl command chomp in C? And if
>there is no exact equivalent command, how would I go about
>removing the "\n" at the end of a stdin?
>Anyway, in answer to the question
>void
chomp(char *x) {
char *p = strrchr(x,'\n');
if(p) *p = 0;
}
perl chomp removes the \n (actually, the current input record seperator)
only at the end of the string (unless you are in paragraph mode),
leaving the string alone if it does not end in \n.

Your chomp routine truncates the string at the last newline in it,
possibly rendering inaccessible characters after that point.

For example, chomp "hello\nthere" in perl would leave the string
alone; in your routine, it would truncate the string to "hello" .
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 3 '06 #19
Jordan Abel wrote:
Are you sure? chomp is used for a specific purpose, which is removing
the newline from a whole line read from input.
That is one common use of chomp(), but it is more accurate to say that
chomp removes zero or one trailing INPUT_RECORD_SEPARATORs from each
element of its argument list, or from each of the the values of a hash
argument.

It can be used for a great many things aside from merely removing a
newline from a string.
Nov 3 '06 #20
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <sl*******************@rlaptop.random.yi.org>,
Jordan Abel <ra*******@gmail.comwrote:

[with respect to perl chomp]
>>Are you sure? chomp is used for a specific purpose, which is removing
the newline from a whole line read from input.

That would be chop, not chomp. chomp removes the newline only
if the newline is there.
Yes, which is consistent with Jordan'd description.

chop (which is the older function) blindly removes the last character
of a string, whether it's a newline or not.

More details to follow elsethread.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 3 '06 #21
Jordan Abel <ra****@random.yi.orgwrites:
[...]
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) ks***@mib.org <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.
Nov 3 '06 #22
2006-11-03 <ln************@nuthaus.mib.org>,
Keith Thompson wrote:
Jordan Abel <ra****@random.yi.orgwrites:
[...]
>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;
}
}
I did mess up.

How about

chomp(s)
char *s;
{
if(s && *s) {
char *p = strrchr(s,0);
if(p[-1]=='\n') {
p[-1]=0;
return 1;
}
}
return 0;
}

may be slightly more efficient than yours if the compiler doesn't get
especially clever with strlen calls.

I threw in null pointer handling because it's practically free.

You could add an int INPUT_RECORD_SEPARATOR; variable, but for it to be
useful you'd need an fgets replacement.
Nov 4 '06 #23
Jordan Abel wrote:
2006-11-03 <ln************@nuthaus.mib.org>,
Keith Thompson wrote:
>Jordan Abel <ra****@random.yi.orgwrites:
[...]
>>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;
}
}

I did mess up.

How about

chomp(s)
char *s;
{
Why use old style function definitions? It is very rare these days to
find a compiler that does not handle prototypes.
int chomp(char *s)
if(s && *s) {
char *p = strrchr(s,0);
I can't see any reason for strrchr to be noticeably faster than strlen
and it could be slower if the compiler does not do clever handling of
that call. After all, it has to find the end of the string before
starting to scan backwards for a value of 0 (which it will find on the
first check) and all strlen has to do is find the end of the string
either counting as it goes or doing a pointer subtraction at the end.
if(p[-1]=='\n') {
p[-1]=0;
return 1;
}
}
return 0;
}

may be slightly more efficient than yours if the compiler doesn't get
especially clever with strlen calls.
I don't think it is likely to be faster. It has the advantage of being
shorter than Keith's but you can achieve that by using

int chomp(char *s)
{
if(s && *s) {
s += strlen(s) - 1;
if (*s == '\n') {
*s = 0;
return 1;
}
}
return 0;
}

I don't expect any significant performance improvement, however if you
want micro-optimisations which is all I see in yours over Keith's I've
removed the need for the extra complexity of strrchr over strlen and
removed the need to index in to an array twice by doing one subtraction.

I prefer a version using strlen to strrchr myself, but this is because
to me it expresses the intent better rather than for reasons of
efficiency since I don't see any of them as having significant
efficiency problems.

Now for an even shorter version that I would *not* put in real code
int chomp(char *s)
{
return (s && *s && *(s += strlen(s) - 1) == '\n')?!(*s = 0):0;
}
I threw in null pointer handling because it's practically free.
Agreed, and that matches the Perl chomp better since the Perl chomp will
not crash on being passed an undef (the nearest Perl equivalent to
passing a null pointer instead of a string).
You could add an int INPUT_RECORD_SEPARATOR; variable, but for it to be
useful you'd need an fgets replacement.
To get the equivalent of Perl reading a line from a file you would need
an fgets replacement anyway, of which a few have been posted to this
group in the past some of which could easily be modified to suit.
--
Flash Gordon
Nov 4 '06 #24
2006-11-04 <r6************@news.flash-gordon.me.uk>,
Flash Gordon wrote:
Why use old style function definitions?
felt like it?
I can't see any reason for strrchr to be noticeably faster than strlen
and it could be slower if the compiler does not do clever handling of
that call. After all, it has to find the end of the string before
starting to scan backwards for a value of 0 (which it will find on the
first check) and all strlen has to do is find the end of the string
either counting as it goes or doing a pointer subtraction at the end.
It's doing a pointer subtraction that we'll then be adding back together
in the caller - I see that as wasteful.

The other bit about efficiency is the original was returning 0 from more
than one place from different 'if' statements.
Nov 4 '06 #25
Keith Thompson wrote:
If you happen to know what Perl's chomp function does
It removes zero or more instances of a globally defined character
equivalence from the end each element of an input list or each element
of the list of values of a hash.

You obviously don't want to deal with the details of an off-topic
language, but you were inaccurate in your description.

Granted, it seems that there are perl programmers who don't know what
chomp actually does, because they only use it in one degenerate case.

Nov 4 '06 #26
Jordan Abel wrote:
Are you sure? chomp is used for a specific purpose, which is removing
the newline from a whole line read from input.
That's like saying printf(...) is used for a specific purpose, which is
putting the characters "Hello, world!" on the console.
Nov 4 '06 #27
2006-11-04 <i9********************@newsfe10.phx>,
james of tucson wrote:
Keith Thompson wrote:
>If you happen to know what Perl's chomp function does

It removes zero or more instances of a globally defined character
equivalence from the end each element of an input list or each element
of the list of values of a hash.

You obviously don't want to deal with the details of an off-topic
language, but you were inaccurate in your description.

Granted, it seems that there are perl programmers who don't know what
chomp actually does, because they only use it in one degenerate case.
Which is thus what it actually does. What it's capable of is a different
matter. Sure it has _potential_ in terms of removing stuff from lists
or hashes or characters other than newline, but cutting the newline off
of single strings, typically those read from input, is its day job.

Regardless, it has nothing to do with "getting rid of all unread data
from a stream up to the next newline" or anything like that.
Nov 4 '06 #28
Jordan Abel wrote:
>Granted, it seems that there are perl programmers who don't know what
chomp actually does, because they only use it in one degenerate case.

Which is thus what it actually does. What it's capable of is a different
matter.
So you'd agree that printf(...) is mainly for putting the characters
"Hello, world!" on the console, then?

I would almost bet that chomp() is implemented as a specialization of
map{}, which doesn't *have* a day job, but is pretty much the most
powerful thing in perl.

Nov 4 '06 #29
Jordan Abel wrote:
2006-11-04 <r6************@news.flash-gordon.me.uk>,
Flash Gordon wrote:
>Why use old style function definitions?

felt like it?
>I can't see any reason for strrchr to be noticeably faster than strlen
and it could be slower if the compiler does not do clever handling of
that call. After all, it has to find the end of the string before
starting to scan backwards for a value of 0 (which it will find on the
first check) and all strlen has to do is find the end of the string
either counting as it goes or doing a pointer subtraction at the end.

It's doing a pointer subtraction that we'll then be adding back together
in the caller - I see that as wasteful.
It may not be doing a pointer subtraction, the processor might be
counting as it goes through (I've certainly used processors where that
could be done at 0 cost in memory or time). As I also said, strrchr has
to then work out that it is at the '/0' character or the compiler has to
special case in some manner strrchr being called to search for the '/0'
that terminates the string. So strrchr is in my opinion at least as
likely to be wasteful.
The other bit about efficiency is the original was returning 0 from more
than one place from different 'if' statements.
Your code had to ifs, Keith's had two, so just as much branching. There
was unlikely to be any significant difference in performance.

Anyway, you didn't comment on my versions one of which only had one
return ;-)
--
Flash Gordon
Nov 4 '06 #30
james of tucson <jmcgill@[go_ahead_and_spam_me].arizona.eduwrites:
Keith Thompson wrote:
>If you happen to know what Perl's chomp function does

It removes zero or more instances of a globally defined character
equivalence from the end each element of an input list or each element
of the list of values of a hash.

You obviously don't want to deal with the details of an off-topic
language, but you were inaccurate in your description.
In the article to which you're replying, I did not attempt to fully
describe what Perl's chomp function does. I inferred what the OP was
trying to do from his description and from the subset of chomp's
behavior of which is easily translated to C. Elsethread, I quoted the
actual definition of chomp from the camel book.

BTW, your description is ambiguous. I don't know what you mean by
"character equivalence", and your description could easily be read to
imply that chomp applied to "hello\n\n" will remove both newline
characters. chomp removes zero or one character from each string.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 4 '06 #31
james of tucson <jmcgill@[go_ahead_and_spam_me].arizona.eduwrites:
Jordan Abel wrote:
>Are you sure? chomp is used for a specific purpose, which is removing
the newline from a whole line read from input.

That's like saying printf(...) is used for a specific purpose, which is
putting the characters "Hello, world!" on the console.
Not really. Probably 99% of calls to chomp in Perl programs do
exactly that (though it's certainly far more versatile). Similarly,
printf() can do far more that print "Hello, world!"; the difference is
that in real programs it usually does.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 4 '06 #32
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:

[with respect to perl chomp]
>and your description could easily be read to
imply that chomp applied to "hello\n\n" will remove both newline
characters. chomp removes zero or one character from each string.
$ perldoc -f chomp
"When in paragraph mode ($/ = ""), it removes all trailing newlines
from the string."
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Nov 5 '06 #33
In article <Fl********************@newsfe10.phx>,
james of tucson <jmcgill@[go_ahead_and_spam_me].arizona.eduwrote:

[perl chomp]
>I would almost bet that chomp() is implemented as a specialization of
map{}, which doesn't *have* a day job, but is pretty much the most
powerful thing in perl.
You'd lose that bet, at least in perl 5.8.4 (and probably much the
same for all earlier versions.) Source file doop.c about lines 1004
to 1139, Perl_do_chomp() routine. It is clearly its own routine,
not a calling upon map and clearly not being a degenerate version
of map. Quite a bit of of the routine is preoccupied with utf8 handling.

--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Nov 5 '06 #34
In article <i9********************@newsfe10.phx>,
james of tucson <jmcgill@[go_ahead_and_spam_me].arizona.eduwrote:

[perl chomp]
>It removes zero or more instances of a globally defined character
equivalence from the end each element of an input list or each element
of the list of values of a hash.
It does not, at least not at perl 5.8.4.

$ perldoc perlvar
$/ [...] You may set it to a multi-character string
to match a multi-character delimiter,
[...] Remember: the value of $/ is a string, not a regexp.
AWK has to be better for something :-)

Not, in other words, a character equivilance: it is a literal match,
except in its treatment of "", undef, and "\n\n" .
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Nov 5 '06 #35
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:

[with respect to perl chomp]
>>and your description could easily be read to
imply that chomp applied to "hello\n\n" will remove both newline
characters. chomp removes zero or one character from each string.

$ perldoc -f chomp
"When in paragraph mode ($/ = ""), it removes all trailing newlines
from the string."
Ok, I missed that (even though I recently posted it myself). But
that's not the way it's usually used, and I don't believe it's what
the OP was looking for.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 5 '06 #36

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fernando Armenta | last post by:
How you cut off the ^M at the end of a line? rstrip is not working. string.rstrip(build_number) thanks,
1
by: Edward WIJAYA | last post by:
Hi, I am new to Python, and I like to learn more about it. Since I am used to Perl before, I would like to know what is Python equivalent of Perl code below: $filename = $ARGV;
3
by: Caj Zell | last post by:
Hello, I am looking a little bit at python and am curious of one thing I often use in perl. Suppose, I want to change the string "best_composer" to "Zappa" in a document called facts.txt, then...
0
by: Aaron Powell | last post by:
I am writing a program for a study I am doing where I must contact several different organisations stored in a database I compiled, but I need to send each email one at a time so they don't know...
2
by: Matt Taylor | last post by:
I have this piece of code: $/ = ' '; $test = "abc "; chomp $test; print $test; which prints spaces at the end of the line. Shouldn't chomp strip the spaces off the end of my scalar $test?
1
by: mrmattborja | last post by:
I have the following lines in a simple for() loop: time_t rightnow; (void) time(&rightnow); fprintf(fp, " waited for pass %d to finish", asctime(localtime(&rightnow)), i) However, when I pull...
3
by: adriaan | last post by:
I'm having this weird problem with chomp I want to get all the numbers out of a file that looks like this 1,2 4,3,2 and have the first number point to an array of the others in a hash I'm almost...
5
by: Beany | last post by:
Hi, Probably a very simple question for the experienced Perl programmers.... Im new to the language of Perl and at the moment im studying the function Chomp! for some strange reason i cant...
4
by: ezechiel | last post by:
Hi, while testing the program (runs in DOS), I thought "if someone hits enter without typing a letter before, what happens?" I tested and the script ends.. Is this normal, or how can I avoid this?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.