chomp () function

Hi

Can someone please explain me with an example of the usage chomp ()
builtin function in perl.

Thanks

Kaushal

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Kaushal Shriyan [ So, 29 August 2010 13:15 ] [ ID #2046824 ]

Re: chomp () function

On Sunday 29 August 2010 14:15:52 Kaushal Shriyan wrote:
> Hi
>
> Can someone please explain me with an example of the usage chomp ()
> builtin function in perl.

Yes, here you go. Let's suppose you want to write a small grep programs that
only prints all lines ending with the character "\". You can write:

[code]
#!/usr/bin/perl

use strict;
use warnings;

while (my $line = <>)
{
# Remove the optional trailing newline character.
chomp($line);

# If $line ends with a backslash - print it.
if ($line =~ m{\\\z})
{
print "$line\n";
}
}
[/code]

Hope it helps.

Regards,

Shlomi Fish

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Funny Anti-Terrorism Story - http://shlom.in/enemy

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ So, 29 August 2010 13:45 ] [ ID #2046825 ]

Re: chomp () function

On Sun, Aug 29, 2010 at 07:15, Kaushal Shriyan <kaushalshriyan [at] gmail.com> wrote:
> Hi
>
> Can someone please explain me with an example of the usage chomp ()
> builtin function in perl.
snip

The chomp function removes whatever is in the $/ variable from the
argument passed in (or $_ if no argument is passed in). The default
value of $/ is "\n". It is often used after reading a line to remove
the newline ("\n"):

open my $fh, "<", "/etc/passwd"
or die "could not open passwd: $!";

while (my $line = <$fh>) {
chomp $line;
print "I read [$line]\n";
}

Note, it will only remove the last string that is equal to $/, so this

{
local $/ = "abc"
my $string = "fooabcbarabcbazabc";
chomp $string;
print "$string\n";
}

will print "fooabcbarabcbaz\n";

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ So, 29 August 2010 14:41 ] [ ID #2046826 ]

Re: chomp () function

On Sun, Aug 29, 2010 at 6:11 PM, Chas. Owens <chas.owens [at] gmail.com> wrote:
> On Sun, Aug 29, 2010 at 07:15, Kaushal Shriyan <kaushalshriyan [at] gmail.com>=
wrote:
>> Hi
>>
>> Can someone please explain me with an example of the usage chomp ()
>> builtin function in perl.
> snip
>
> The chomp function removes whatever is in the $/ variable from the
> argument passed in (or $_ if no argument is passed in). =A0The default
> value of $/ is "\n". =A0It is often used after reading a line to remove
> the newline ("\n"):
>

Hi Chas

$/ variable is a line separator variable whereas $_ is a default
variable. Please explain me when you said no argument is passed
so what i understand is chomp $line means $line is a argument ?

Thanks

Kaushal

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Kaushal Shriyan [ So, 29 August 2010 15:23 ] [ ID #2046827 ]

Re: chomp () function

On Sun, Aug 29, 2010 at 09:23, Kaushal Shriyan <kaushalshriyan [at] gmail.com> w=
rote:
> On Sun, Aug 29, 2010 at 6:11 PM, Chas. Owens <chas.owens [at] gmail.com> wrote=
:
>> On Sun, Aug 29, 2010 at 07:15, Kaushal Shriyan <kaushalshriyan [at] gmail.com=
> wrote:
>>> Hi
>>>
>>> Can someone please explain me with an example of the usage chomp ()
>>> builtin function in perl.
>> snip
>>
>> The chomp function removes whatever is in the $/ variable from the
>> argument passed in (or $_ if no argument is passed in). =C2=A0The defaul=
t
>> value of $/ is "\n". =C2=A0It is often used after reading a line to remo=
ve
>> the newline ("\n"):
>>
>
> Hi Chas
>
> $/ variable is a line separator variable whereas $_ is a default
> variable. Please explain me when you said no argument is passed
> so what i understand is chomp $line means $line is a argument ?
>
> Thanks
>
> Kaushal
>

Yes, $line is an argument that is passed to chomp in the code I sent
earlier. When you say

chomp $line;

then the $line variable will be affected, but if you say

chomp;

then the $_ variable will be affected.

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ So, 29 August 2010 15:36 ] [ ID #2046828 ]

Re: chomp () function

On Sun, Aug 29, 2010 at 7:06 PM, Chas. Owens <chas.owens [at] gmail.com> wrote:
> On Sun, Aug 29, 2010 at 09:23, Kaushal Shriyan <kaushalshriyan [at] gmail.com>=
wrote:
>> On Sun, Aug 29, 2010 at 6:11 PM, Chas. Owens <chas.owens [at] gmail.com> wrot=
e:
>>> On Sun, Aug 29, 2010 at 07:15, Kaushal Shriyan <kaushalshriyan [at] gmail.co=
m> wrote:
>>>> Hi
>>>>
>>>> Can someone please explain me with an example of the usage chomp ()
>>>> builtin function in perl.
>>> snip
>>>
>>> The chomp function removes whatever is in the $/ variable from the
>>> argument passed in (or $_ if no argument is passed in). =A0The default
>>> value of $/ is "\n". =A0It is often used after reading a line to remove
>>> the newline ("\n"):
>>>
>>
>> Hi Chas
>>
>> $/ variable is a line separator variable whereas $_ is a default
>> variable. Please explain me when you said no argument is passed
>> so what i understand is chomp $line means $line is a argument ?
>>
>> Thanks
>>
>> Kaushal
>>
>
> Yes, $line is an argument that is passed to chomp in the code I sent
> earlier. =A0When you say
>
> chomp $line;
>
> then the $line variable will be affected, but if you say
>
> chomp;
>
> then the $_ variable will be affected.
>

Hi Chas

Thanks a lot Chas. Understood now.
Also what does $_ default variable means exactly, any example would
really help me understand it

Thanks

Kaushal

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Kaushal Shriyan [ So, 29 August 2010 15:41 ] [ ID #2046829 ]

Re: chomp () function

On Sun, Aug 29, 2010 at 09:41, Kaushal Shriyan <kaushalshriyan [at] gmail.com> wrote:
snip
> Thanks a lot Chas. Understood now.
> Also what does $_ default variable means exactly, any example would
> really help me understand it
snip

The default variable is set or read by many Perl 5 functions. For
instance, if you use the readline operator (<>) in a while loop's test
it sets $_ and print uses $_ if no arguments are passed to it, so

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
print;
}

__DATA__
foo
bar
baz

will print the lines "foo\n", "bar\n", and "baz\n". $_ is also the
default target for regexes, so

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
next unless /b/;
print;
}

__DATA__
foo
bar
baz

Will print "bar\n" and "baz\n", but not "foo\n" because it does not
match the regex.

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ So, 29 August 2010 16:28 ] [ ID #2046830 ]

Re: chomp () function

On Sun, Aug 29, 2010 at 7:58 PM, Chas. Owens <chas.owens [at] gmail.com> wrote:
> On Sun, Aug 29, 2010 at 09:41, Kaushal Shriyan <kaushalshriyan [at] gmail.com>=
wrote:
> snip
>> Thanks a lot Chas. Understood now.
>> Also what does $_ default variable means exactly, any example would
>> really help me understand it
> snip
>
> The default variable is set or read by many Perl 5 functions. =A0For
> instance, if you use the readline operator (<>) in a while loop's test
> it sets $_ and print uses $_ if no arguments are passed to it, so
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> while (<DATA>) {
> =A0 =A0print;
> }
>
> __DATA__
> foo
> bar
> baz
>
> will print the lines "foo\n", "bar\n", and "baz\n". =A0$_ is also the
> default target for regexes, so
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> while (<DATA>) {
> =A0 =A0next unless /b/;
> =A0 =A0print;
> }
>
> __DATA__
> foo
> bar
> baz
>
> Will print "bar\n" and "baz\n", but not "foo\n" because it does not
> match the regex.
>
> --
> Chas. Owens
> wonkden.net
> The most important skill a programmer can have is the ability to read.
>

Hi Chas,

Thanks Chas.
Much appreciated :-)

I am referring to http://www.perl.org/books/beginning-perl/. so I am
planning to read upto chapter 10. is it recommended to even go beyond
chapter 10.
Please suggest.

How do i start writing my own perl scripts. Please suggest. Do you
recommend something else.

Kaushal

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Kaushal Shriyan [ So, 29 August 2010 16:47 ] [ ID #2046831 ]

Re: chomp () function

At 8:17 PM +0530 8/29/10, Kaushal Shriyan wrote:
>
>I am referring to http://www.perl.org/books/beginning-perl/. so I am
>planning to read upto chapter 10. is it recommended to even go beyond
>chapter 10.
>Please suggest.


I have not read that online resource, but looking at the contents I
would recommend you first read and learn chapters 1-9. Chapter 10
begins Modules, which is a more advanced Perl topic and, while
important, you can do a lot of Perl learning without them. Some very
useful modules come with all Perl distributions, and you can use
those by following examples that come with the module documentation.
Every module has its own documentation.

>
>How do i start writing my own perl scripts. Please suggest. Do you
>recommend something else.

You need to get a Perl distribution, if you don't already have one.
Which one depends upon your platform (operating system). There are
several for Windows, and one for most other OSes. Go to www.perl.org
and start downloading. You will also need a text editor, such as vi,
emacs, notepad, etc. depending once again on your platform.

Your Perl distribution will come with built-in documentation, usually
in the form of the command-line program 'perldoc' or possibly a set
of HTML files. Learn how to use it. Most of your questions so far to
this list are answered in the built-in documentation.

Good luck.

--
Jim Gibson
Jim [at] Gibson.org

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ So, 29 August 2010 19:17 ] [ ID #2046832 ]
Perl » gmane.comp.lang.perl.beginners » chomp () function

Vorheriges Thema: Imp
Nächstes Thema: Perl Recaptcha