airnet
airnet

Reputation: 2673

chomp giving me 1's and 0's

I have two pieces of code. The first one is what I wanted. BUt why is it the second one is giving me 1's and 0's (is my english correct, or is it "1s and 0s") and not "johnchrismandy".


foreach (@data) {
    print ;
}
//output
john
chris
mandy


foreach  (@data) {
    print chomp ;
}
//output
110

UPDATE:: Thank you guys, I understand it more now. But I don't understand the last part of the doc.

=> You can actually chomp anything that's an lvalue, including an assignment: chomp($cwd = pwd);

Upvotes: 2

Views: 3804

Answers (4)

Jeff Ward
Jeff Ward

Reputation: 19136

As others have stated, chomp returns the number of characters removed. In my particular instance (regex with eval modifier in a one-line perl replace-in-file statement), I needed to get the chomped value in a single statement without having a separate print statement. I finally found a working solution - wrapping the chomp command in an if statement.

Starting with:

$in = "I have COMMITS commits in my log";
$in =~ s/COMMITS/`git log | grep -i '^commit' | wc -l`/e;
print $in;

This returns:

I have 256
 commits in my log

Great, I need to chomp this, so I try:

$in = "I have COMMITS commits in my log";
$in =~ s/COMMITS/chomp `git log | grep -i '^commit' | wc -l`/e;
print $in;

But this throws an error:

Can't modify quoted execution (``, qx) in chomp at ./script line 4, near "`git log | grep -i '^commit' | wc -l`}"
Execution of ./script aborted due to compilation errors.

Yeesh, so I need to assign the output to a local var and chomp it:

$in = "I have COMMITS commits in my log";
$in =~ s/COMMITS/chomp (my $VAR = `git log | grep -i '^commit' | wc -l`)/e;
print $in;

But as we said, chomp returns # of characters stripped:

I have 1 commits in my log

Then I found that I could wrap it in an if statement and get it to return the result, chomped:

$in = "I have COMMITS commits in my log";
$in =~ s/COMMITS/if (chomp (my $VAR = `git log | grep -i '^commit' | wc -l`)) { $VAR }/e;
print $in;

Finally, I get the command result, chomped, in one statement:

I have 256 commits in my log

Upvotes: 0

ikegami
ikegami

Reputation: 386396

This is documented behaviour: "It returns the total number of characters removed from all its arguments." You want

for (@data) {
   chomp;
   print "$_\n";
}

Note that $_ is aliased to the element of @data, so @data is getting modified too. If you don't want that to happen.

for (@data) {
   my $item = $_;
   chomp($item);
   print "$item\n";
}

About the last line of the docs:

my $item = $_; returns $item as an lvalue (a value suitable for the left-hand side of an assignment). As such,

my $item = $_;
chomp($item);

can be written as

chomp( my $item = $_ );

Upvotes: 6

Zagorax
Zagorax

Reputation: 11890

chomp returns the total number of character removed.

So it prints how many \n it has removed.

Do it in the following way:

foreach  (@data) {
    chomp($_);
    print $_;
}

Upvotes: 1

mathematician1975
mathematician1975

Reputation: 21351

This is because you are printing the return value of the chomp function and this is the total number of characters removed from all its arguments

Upvotes: 1

Related Questions