Previous Thread
Next Thread
Print Thread
Rate Thread
#101164 09/08/2000 12:31 AM
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
There was a thread here the other day that got lost with the recent crash.

If I remember correctly, the question was what exactly does chomp do? This answers given were only partially right.

Chomp removes the last character of a string, if that character matches what $/ set to. $/ is set to n by default, so when you see chomp $myvar it is looking to remove a n from the end. If the end is not a n, nothing gets removed. This differs from chop in that the last characer, no matter what it is, is removed by chop.

If you have slurped an entire file into an array and want to remove the n from each element, simply doing chomp @myarray will take care of that.

perldoc -f chomp for more information

--mark



------------------


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
Sponsored Links
Joined: Feb 2000
Posts: 2,668
Deb Offline
Pooh-Bah
Pooh-Bah
Offline
Joined: Feb 2000
Posts: 2,668
Thank you for the clarification Mark... it's very much appreciated! [Linked Image]


- Deb
Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
Yeah thanks Mark, that was the answer I really needed. I get it now...Thanks again!

------------------
Quality Pages designing the world professional web pages for affordable prices.

Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
Wait, actaully can someone please tell me why a chomp is needed in this small script?

#!/usr/bin/perl
$classname = "CGI Programming 101";
print "Hello there. What is your name?n";
$you = ;
chomp($you);
print "Hello, $you. Welcome to $classname.n";

Thanks, b/c Mark you said that if a character matches $/ Perl will automatically (by default) put a n right behind it right??

------------------
Quality Pages designing the world professional web pages for affordable prices.

Joined: Aug 2000
Posts: 3,590
Moderator
Moderator
Offline
Joined: Aug 2000
Posts: 3,590
When entering data via the standard input command, the user must hit enter to proceed, which means a n is also in the string. chomp is needed to remove it.

Feel free to correct Mark.

------------------
I wish I were a Canuck
All they have to do is drink beer, play hockey, and say "Eh"

* No offense meant to any Canadians

Sponsored Links
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
Atom, correct. When inputting via STDIN, hitting enter places a n on the end of your string, which must be chomp'd.

Quote
quote:
No, if the last character of the string matches what $/ is set to, it will be removed when chomp is called. $/ is set to n by default, but can be changed by the programmer if need be.

so if I have two strings:

$s1 = "My name is Markn";
$s2 = "blah blah blah";

chomp $s1;

will remove the n.

chomp $s2;

will do nothing to the string

--mark


------------------


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
Joined: Mar 2000
Posts: 445
Member
Member
Offline
Joined: Mar 2000
Posts: 445
Ok thanks Mark & atom911, I get it now!
[Linked Image]

------------------
Quality Pages designing the world professional web pages for affordable prices.


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
hatter
hatter
USA
Posts: 69
Joined: January 2001
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)