Sunday 29 December 2013

Chop vs Chomp in Perl

In Perl, the basic difference between the keywords 'chop' and 'chomp' is as follows:

Chop is used to literally chop the last character of a string. It simply deletes the last character.

Eg:

chop garlic;

The output will be garli with 'c' getting chopped off :)

On the other hand, Chomp is used to delete the endline character "\n";

Eg:

$var = "garlic\n";

chomp $var;

The resultant value of $var will be the same string garlic, but with only the newline character getting deleted.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker