php forum
php mysql forum
php mysql smarty
 
Topic Options
#101164 - 09/08/00 12:31 AM Answer to the chomp question
Mark Badolato Offline
P.I.T.A. / Programmer

Registered: 09/08/00
Posts: 836
Loc: Land O' Cactus
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

Top
#101165 - 09/08/00 02:06 PM Re: Answer to the chomp question
Deb Offline

Pooh-Bah

Registered: 02/13/00
Posts: 3480
Loc: Wisconsin
Thank you for the clarification Mark... it's very much appreciated!
_________________________
- Deb

Top
#101166 - 09/08/00 02:57 PM Re: Answer to the chomp question
Pyros Offline
Member

Registered: 03/10/00
Posts: 1070
Loc: Ontario, Canada
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.
_________________________
Quality Pages designing the world professional web pages for affordable prices.

Top
#101167 - 09/08/00 03:05 PM Re: Answer to the chomp question
Pyros Offline
Member

Registered: 03/10/00
Posts: 1070
Loc: Ontario, Canada
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 = <STDIN>;
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.
_________________________
Quality Pages designing the world professional web pages for affordable prices.

Top
#101168 - 09/08/00 10:29 PM Re: Answer to the chomp question
The Team Offline
Moderator

Registered: 08/11/00
Posts: 182
Loc: yes
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

Top
#101169 - 09/09/00 12:23 AM Re: Answer to the chomp question
Mark Badolato Offline
P.I.T.A. / Programmer

Registered: 09/08/00
Posts: 836
Loc: Land O' Cactus
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

Top
#101170 - 09/09/00 01:42 PM Re: Answer to the chomp question
Pyros Offline
Member

Registered: 03/10/00
Posts: 1070
Loc: Ontario, Canada
Ok thanks Mark & atom911, I get it now!


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

Top



Latest Posts
[7.2.1] - Naked shoutbox
by bellaonline
05/05/12 05:00 PM
[7.x] Stop Forum Spam Integration v0.4
by bellaonline
05/05/12 03:53 PM
Shout Box

(Views)Popular Topics
Known public proxy servers 1689885
Integrated Index Page (IIP) 5.3.1 555705
Finished-[6.5.2] Games Arcade Deluxe v1.9 501236
Integrated Index Page (IIP) 5.1.1 415112
TLD Bv2.1 Released - Threads Links Directory 396822
[6.0x] Who's Online 4.0.0 [Finished] 389412
Finished-[6.5.1] Integrated Index Page (IIP) 6.5 330423
Q & A 298663
Slash UBB 266936
[6.3.x] [beta] Hit Hack 2.0 227970
Forum Stats
13621 Members
59 Forums
37191 Topics
295716 Posts

Max Online: 686 @ 06/28/07 07:04 AM

 

 

 
fusionbb message board php hacks