For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2006 > Chomp method









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Chomp method
AndrewMcHorney

2006-04-27, 3:58 am

Hello

I am wondering how the chomp function works. I am looking to see how
I can truncate new line and/or carriage returns from a string. Will
chomp do nothing if there are no carriage return or line feed at the
end. If the end of the string has a space will it leave it alone?

Andrew


Nishanth Ev

2006-04-27, 3:58 am

Hello,

The chomp function will chomp off the last character
if its a new line.
Spaces wont be truncated by chomp.

Regards
Nishanth


> Hello
>
> I am wondering how the chomp function works. I am
> looking to see how
> I can truncate new line and/or carriage returns from
> a string. Will
> chomp do nothing if there are no carriage return or
> line feed at the
> end. If the end of the string has a space will it
> leave it alone?
>
> Andrew
>
>
>
> --
> To unsubscribe, e-mail:
> beginners-unsubscribe@perl.org
> For additional commands, e-mail:
> beginners-help@perl.org
> <http://learn.perl.org/>
> <http://learn.perl.org/first-response>
>
>
>



________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
usenet@DavidFilmer.com

2006-04-27, 3:58 am

AndrewMcHorney wrote:
> I am wondering how the chomp function works.


You can ask Perl: perldoc -f chomp

It will tell you that chomp removes whatever you have defined as your
input record separator ($/). You could assign $/ to ANYTHING and chomp
would remove it. For example:

$/ = "bar";
chomp (my $string = 'foobar');
print $string;

and chomp would remove "bar" and it would print "foo".

chomp will not remove anything EXCEPT $/. Usually this variable is set
to whatever newline your system uses.

--
http://DavidFilmer.com

Jaime Murillo

2006-04-27, 3:58 am

On Wednesday 26 April 2006 22:51, AndrewMcHorney wrote:
> Hello

Hey Andrew

>
> I am wondering how the chomp function works. I am looking to see how
> I can truncate new line and/or carriage returns from a string. Will
> chomp do nothing if there are no carriage return or line feed at the
> end. If the end of the string has a space will it leave it alone?


The good way to learn how a function works is to use the perldoc utility.

perldoc -f <function>

>
> Andrew

John W. Krahn

2006-04-27, 3:58 am

AndrewMcHorney wrote:
> Hello


Hello,

> I am wondering how the chomp function works. I am looking to see how I
> can truncate new line and/or carriage returns from a string. Will chomp
> do nothing if there are no carriage return or line feed at the end. If
> the end of the string has a space will it leave it alone?


If $/ contains a string then chomp() removes the contents of the $/ variable
from the end of the string and it returns a count of the number of $/ that
were removed. If $/ contains undef or a reference then chomp() does nothing.



John
--
use Perl;
program
fulfillment
Mr. Shawn H. Corey

2006-04-27, 7:57 am

On Wed, 2006-26-04 at 23:07 -0700, Jaime Murillo wrote:
> The good way to learn how a function works is to use the perldoc utility.


A good way to learn how a function works is to write a small program to
test it. There is nothing like hands-on experience.


--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/


Chad Perrin

2006-04-27, 7:57 am

On Thu, Apr 27, 2006 at 07:25:15AM -0400, Mr. Shawn H. Corey wrote:
> On Wed, 2006-26-04 at 23:07 -0700, Jaime Murillo wrote:
>
> A good way to learn how a function works is to write a small program to
> test it. There is nothing like hands-on experience.


.. . . or "Apply the T.I.T.S. principle."

http://www.perlmonks.org/?node_id=545998

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
"There comes a time in the history of any project when it becomes necessary
to shoot the engineers and begin production." - MacUser, November 1990
Richard Copits

2006-04-27, 7:57 am

I'd have to say that I disagree with this approach - as mentioned by
several people here...

Although the "try it and see" approach is fun and intellectually
challenging, it really isn't productive for everyone. For example, I
wouldn't like to be paying a contractor who is getting paid per hour to
do a lot of "try it and see" as I don't really want to pay for his/her
exploration of dead ends and blind alleys.=20

For a LOT of people, having one working example is a good way to learn
and build upon. For example, if I want to build a bookcase, I'd much
rather be able to look at an existing bookcase that is well-built and
stable then be requested to "try it and see" with a hammer, some nails
and a bunch of boards. Sure, I'll eventually probably come up with
something that may resemble a bookcase and may indeed last, but being
able to copy an existing bookcase would be a far better use of my time.=20=


A "guide" is always a better way to learn - that's why we have training.


I am sensitive to the comment that there are indeed some people who
don't want to use a good example to learn from, but just have someone
else do their work for them, but I also believe that this kind of person
is pretty easy to recognize by their comments.....

Anyway, just a lurkers opinion....who has learned a LOT by building on
working examples - AND - has learned a LOT by doing it that way....


"The man who sets out to carry a cat by its tail learns something that
will always be useful and which will never grow dim or doubtful."

Mark Twain


-----Original Message-----
From: Chad Perrin [mailto:perrin@apotheon.com]=20
Sent: Thursday, April 27, 2006 7:55 AM
To: beginners@perl.org
Subject: Re: Chomp method

On Thu, Apr 27, 2006 at 07:25:15AM -0400, Mr. Shawn H. Corey wrote:
> On Wed, 2006-26-04 at 23:07 -0700, Jaime Murillo wrote:
utility.[color=darkred]
>=20
> A good way to learn how a function works is to write a small program

to
> test it. There is nothing like hands-on experience.


=2E. . . or "Apply the T.I.T.S. principle."

http://www.perlmonks.org/?node_id=3D545998

--=20
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
"There comes a time in the history of any project when it becomes
necessary
to shoot the engineers and begin production." - MacUser, November 1990

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>





Portions of this message may be confidential under an exemption to Ohio's=
public records law or under a legal privilege. If you have received this=
message in error or due to an unauthorized transmission or interception,=
please delete all copies from your system without disclosing, copying, o=
r transmitting this message.
Russ Foster

2006-04-27, 7:57 am

Great article! Thanks for the reference.

-r

> -----Original Message-----
> From: Chad Perrin [mailto:perrin@apotheon.com]
>
> . . . or "Apply the T.I.T.S. principle."
>
> http://www.perlmonks.org/?node_id=545998
>




Randal L. Schwartz

2006-04-27, 6:58 pm

>>>>> ""Mr" == "Mr Shawn H Corey" <shawnhcorey@magma.ca> writes:

"Mr> On Wed, 2006-26-04 at 23:07 -0700, Jaime Murillo wrote:[color=darkred]

"Mr> A good way to learn how a function works is to write a small program to
"Mr> test it. There is nothing like hands-on experience.

Unless your experience doesn't stumble across the corner cases. Nothing
can replace reading a good specification.

For example, almost no amount of experimentation will stumble across
how chomp actually removes $/, not just "\n".

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Ron Goral

2006-04-27, 6:58 pm



> -----Original Message-----
> From: Randal L. Schwartz [mailto:merlyn@stonehenge.com]
> Sent: Thursday, 27 April, 2006 10:18
> To: beginners@perl.org
> Subject: Re: Chomp method
>
>
> For example, almost no amount of experimentation will stumble across
> how chomp actually removes $/, not just "\n".
>


Does this mean it resets the default record separator or just removes the
one that is at the end of the line/record?

Peace -
Ron


Russ Foster

2006-04-27, 6:58 pm

> -----Original Message-----
> From: Randal L. Schwartz [mailto:merlyn@stonehenge.com]
>
> For example, almost no amount of experimentation will stumble across
> how chomp actually removes $/, not just "\n".



But that's exactly why you need to try it and see. The more a document
spends on the arcane details of a function (which perldoc does), the more
likely a beginner would get lost in those details and find some other source
of reference.

Instead of the perldoc chomp example of:

while (<> ) {
chomp; # avoid \n on last field
@array = split(/:/);
# ...
}

(I consider myself an intermediate Perl programmer, and the above is still a
useless example to me. It never really shows you what chomp does).

How about :

$ll = "A full line here.\n" ;
chomp ( $ll ) ;

# Now $ll = "A full line here." ;

Sure this doesn't cover everything but for a beginner it's an example you
can put your arms around.

-r

Russell J. Foster
R. J. Foster and Associates
rjf@russfoster.com



Randal L. Schwartz

2006-04-27, 6:58 pm

>>>>> "Russ" == Russ Foster <rjf@russfoster.com> writes:

Russ> But that's exactly why you need to try it and see. The more a document
Russ> spends on the arcane details of a function (which perldoc does), the more
Russ> likely a beginner would get lost in those details and find some other source
Russ> of reference.

And *that's* the difference between a reference doc (like the manpage) and
a good tutorial (I seem to recall a few of those).

However, you didn't challenge my point. You merely made a different point.
It is *not* sufficient to merely experiment. Reading the reference doc is
mandatory for complete understanding.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Russ Foster

2006-04-27, 6:58 pm

> -----Original Message-----
> From: Randal L. Schwartz [mailto:merlyn@stonehenge.com]
>
> However, you didn't challenge my point. You merely made a different
> point.
> It is *not* sufficient to merely experiment. Reading the reference doc is
> mandatory for complete understanding.


That's because I'm not (completely) against your point. ;-)

However, the original question was:

<<< I am wondering how the chomp function works. I am looking to see how I
can truncate new line and/or carriage returns from a string. Will chomp do
nothing if there are no carriage return or line feed at the end. If the end
of the string has a space will it leave it alone? >>>

Which is more effective: (1) developing a reference document that covers all
possible questions? Or (2) Writing six lines of code to test it?

As soon as someone discovers the answer to the above referenced question,
they'll want to know if chomp removes a trailing \r, or \t or space or ....

-r

Russell J Foster
R. J. Foster and Associates
rjf@russfoster.com


Tom Phoenix

2006-04-27, 6:58 pm

On 4/27/06, Russ Foster <rjf@russfoster.com> wrote:

> Instead of the perldoc chomp example of:


It sounds as if you have ideas on how to improve the Perl
documentation. I heartily encourage you to contribute your
improvements by means of the perlbug program, which comes with Perl. A
patch to the .pod files is generally the best way to contribute
documentation.

Cheers!

--Tom Phoenix
Stonehenge Perl Training
Russ Foster

2006-04-27, 6:58 pm


> -----Original Message-----
> From: tom.phoenix@gmail.com [mailto:tom.phoenix@gmail.com] On Behalf Of
>
>
> It sounds as if you have ideas on how to improve the Perl
> documentation. I heartily encourage you to contribute your
> improvements by means of the perlbug program, which comes with Perl. A
> patch to the .pod files is generally the best way to contribute
> documentation.


Maybe I'll just buy a good reference book. ;-)

-r

Russell J. Foster
Systems Management Consultant
rjf@russfoster.com



Mr. Shawn H. Corey

2006-04-27, 6:58 pm

On Thu, 2006-27-04 at 08:18 -0700, Randal L. Schwartz wrote:
> "Mr> A good way to learn how a function works is to write a small program to
> "Mr> test it. There is nothing like hands-on experience.
>
> Unless your experience doesn't stumble across the corner cases. Nothing
> can replace reading a good specification.


I meant that they should use hands-on experiences to augment their
reading, not to replace it.

I realize that programmers are nit-pickers (it's an occupational hazard)
and they assumed that leaving something out implies it will be ignored
altogether but people, unlike computers, have the amazing ability to
fill in the gaps. But to make you happy:

Everyone should compliment their reading with experimentation and their
experimentation with reading.


--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/


Chad Perrin

2006-04-27, 6:58 pm

On Thu, Apr 27, 2006 at 08:23:52AM -0400, Richard.Copits@bwc.state.oh.us wrote:
>
> Although the "try it and see" approach is fun and intellectually
> challenging, it really isn't productive for everyone. For example, I
> wouldn't like to be paying a contractor who is getting paid per hour to
> do a lot of "try it and see" as I don't really want to pay for his/her
> exploration of dead ends and blind alleys.


I think the assumption is that when you contract someone to write code
for you, you're paying for someone that knows what he's doing as opposed
to a rank neophyte (unless you're only paying enough for a rank
neophyte).


>
> A "guide" is always a better way to learn - that's why we have training.


I disagree. I'd say, rather, that a "guide" is better than no "guide",
all else being equal. Taking a Try It To see approach is likewise
better than a passive approach, all else being equal. Combining the two
is probably best of all -- usually, by trying something so that you KNOW
what you're going to ask about when you start looking for a "guide".


>
> I am sensitive to the comment that there are indeed some people who
> don't want to use a good example to learn from, but just have someone
> else do their work for them, but I also believe that this kind of person
> is pretty easy to recognize by their comments.....


Yeah, usually those who aren't just looking for a free pass have already
tried a couple things and done some research.

I'm not saying you're wrong about the value of working examples -- just
that working examples should often come after the try-and-fail approach.
One typically learns more from failures than successes (which is
primarily useful if the failures aren't catastrophic and fatal), though
successes are more conducive to meeting immediate goals in a timely
manner. A balance is needed, and it seems that balance is best reached
by trying things before asking for the answer.

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
Ben Franklin: "As we enjoy great Advantages from the Inventions of
others we should be glad of an Opportunity to serve others by any
Invention of ours, and this we should do freely and generously."
DJ Stunks

2006-04-27, 6:58 pm


Chad Perrin wrote:
> Shawn H. Corey wrote:
>
> "Apply the T.I.T.S. principle."


I don't think there's anything better in this world than hands-on
T.I.T.S.

-jp

Randal L. Schwartz

2006-04-28, 6:58 pm

>>>>> ""Mr" == "Mr Shawn H Corey" <shawnhcorey@magma.ca> writes:

"Mr> Everyone should compliment their reading with experimentation and their
"Mr> experimentation with reading.

Do you mean "complement", or do you mean that you should say kind words
about your reading? :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Mr. Shawn H. Corey

2006-04-28, 6:58 pm

On Fri, 2006-28-04 at 07:33 -0700, Randal L. Schwartz wrote:
>
> "Mr> Everyone should compliment their reading with experimentation and their
> "Mr> experimentation with reading.
>
> Do you mean "complement", or do you mean that you should say kind words
> about your reading? :)


Oops. This just goes to show that that your works not done even with a
good spelling checker. You should complement your reading and compliment
the author(s). BTW, thank you for writing such great books.


--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/


Chad Perrin

2006-04-28, 6:58 pm

On Fri, Apr 28, 2006 at 11:05:23AM -0400, Mr. Shawn H. Corey wrote:
> On Fri, 2006-28-04 at 07:33 -0700, Randal L. Schwartz wrote:
>
> Oops. This just goes to show that that your works not done even with a
> good spelling checker. You should complement your reading and compliment
> the author(s). BTW, thank you for writing such great books.


Ironic -- isn't it? Repetitions of "that", no apostrophe in "work's".
Actually, I think that's where I got that extraneous apostrophe that
snuck into the word "its" in another email of mine a few minutes ago. I
don't know if I stole it from you, or if you used it to sabotage me, but
either way you can have it back now.

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]
print substr("Just another Perl hacker", 0, -2);
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com