For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2007 > using chomp with array element.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author using chomp with array element.
pipehappy

2007-02-28, 6:59 pm

Hi

I have a sample like this.

@fs = `ls -p`;
$haha = $fs[1];
chomp($haha);
print $haha;
print "\n";
print $fs[1];
print chomp($fs[1]);

and the output is:

2006/
2006/
1

Could someone please explain me that the final result is not the
expected value 2006/ but the 1. It is not what I expect. Thanks.

usenet@DavidFilmer.com

2007-02-28, 6:59 pm

On Feb 28, 2:56 pm, "pipehappy" <pipeha...@gmail.com> wrote:

> print chomp($fs[1]);


chomp() acts on the variable but returns the total number of
characters that were chompped. So chomp() chompped off a newline (one
character) and returned that value to your print statement.

You would need to chomp the variable and then print its value, such
as:

chomp($fs[1]);
print $fs[1];


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com