Gossamer Forum
Home : General : Perl Programming :

chop and chomp -- wt?'s new line character???

Quote Reply
chop and chomp -- wt?'s new line character???
chop, chomp, chop, chomp, and chop, chomp???Mad

Ok, i understand, 'chop' and 'chomp' are used to remove the newline character, but what exactly is this 'newline character'???

Can anybody please gimme a clear explanation on that? I'll be really thankrul to u.
Quote Reply
Re: [Malik] chop and chomp -- wt?'s new line character??? In reply to
A newline charachter is something like \t or \n. Basically, it means that a newline would be inserted into the output. Here are some example scripts;

Code:
#!/usr/bin/perl

$var = qq|

this
is
my test
variables

|;

$var =~ s/[\t\n]/g;

# $var now holds;
# thisismytestvariables
# All the newlines have been removed....

Hope that helps Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] chop and chomp -- wt?'s new line character??? In reply to
Eh?....\t is a tab, not a newline Wink

Also your example didn't even use chomp (and contains syntax errors) Sly

Last edited by:

Paul: Jul 11, 2003, 2:12 AM
Quote Reply
Re: [Malik] chop and chomp -- wt?'s new line character??? In reply to
No, chop is used to remove the last character in a string. chomp is used to remove newlines.
Quote Reply
Re: [Paul] chop and chomp -- wt?'s new line character??? In reply to
>>>Also your example didn't even use chomp (and contains syntax errors) <<<

I was trying to show what newlines did Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] chop and chomp -- wt?'s new line character??? In reply to
Thanx Andy, Thanx Paul. But, from your example Andy i could see removing the newline characters only make a mess. So why we do that?

Can you guys please tell me how this comes into use? or how is this useful?

Please dont mind if this sounds too stupid of a question, but i really wanna know.
Quote Reply
Re: [Malik] chop and chomp -- wt?'s new line character??? In reply to
Well, for example, when we have a newline on the end of a variable, and we want to use it to write a file. We obviously can't have spaces in it it, or newlins breaks, otherwise it would cause an error.

Another exaple. You want to format your perl script so the HTML appears all tidy. You could use something like;

print "<html>\n";
print "<body>\n";

to make it appaer like;

<html>
<body>

..and not...

<html><body>

Just a couple of examples as to its use. Its one of those kind of features that you don't use, until you really need it (when you get more into programming)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] chop and chomp -- wt?'s new line character??? In reply to
Thanx Andy.

It's yet not completely clear to me but one thing i understood is that i can start 'chop'ing and 'chomp'ing with complete understanding once i am into real programin'.Crazy

Thanx once again.
Quote Reply
Re: [Paul] chop and chomp -- wt?'s new line character??? In reply to
In Reply To:
No, chop is used to remove the last character in a string. chomp is used to remove newlines.

Actually chomp is used to remove whatever character $/ is set to, which defaults to newline (\n)

--mark
Quote Reply
Re: [Mark Badolato] chop and chomp -- wt?'s new line character??? In reply to
LOL.... finally Paul gets a taste of his own medicine (re making someone look like an idiot cos they didn't get something totally right) Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Mark Badolato] chop and chomp -- wt?'s new line character??? In reply to
Yeah I know, but I didn't feel like going into all that. I was actually reading the perldoc before replying.
Quote Reply
Re: [Mark Badolato] chop and chomp -- wt?'s new line character??? In reply to
Quote:
Actually chomp is used to remove whatever character $/ is set to, which defaults to newline (\n)

Actually it defaults to whatever the o/s uses as a newline, on windows that will be \r\n. =) Sorry, couldn't resist.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] chop and chomp -- wt?'s new line character??? In reply to
ah tis true. btw, i wasnt trying to give paul the smackdown :) :)
Quote Reply
Re: [Mark Badolato] chop and chomp -- wt?'s new line character??? In reply to
I wouldn't have minded if you were, I'm used to it Wink