|
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
|
P.I.T.A. / Programmer
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
|
|
|
|
Joined: Feb 2000
Posts: 2,668
Pooh-Bah
|
Pooh-Bah
Joined: Feb 2000
Posts: 2,668 |
Thank you for the clarification Mark... it's very much appreciated! ![[Linked Image]](https://ubbdev.com/ubb/smile.gif)
- Deb
|
|
|
|
Joined: Mar 2000
Posts: 445
Member
|
Member
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
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
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
|
|
|
|
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
|
P.I.T.A. / Programmer
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. 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
Joined: Mar 2000
Posts: 445 |
Ok thanks Mark & atom911, I get it now! ![[Linked Image]](https://ubbdev.com/ubb/smilies//smile.gif) ------------------ Quality Pages designing the world professional web pages for affordable prices.
|
|
|
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.
|
|
Posts: 254
Joined: January 2000
|
|
Forums63
Topics37,575
Posts293,931
Members13,824
|
Most Online6,139 Sep 21st, 2024
|
|
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
|
|
|
|