RE: chomp operator



Hi,


Hello,

so where does the chomp operator plays its role, can some one
explain me here with a sample of code.

Thanks and Regards

Kaushal


One place where chomp() comes in handy is when you're reading from a
file.
Usually a line read from a file will have a newline at the end.

The terminating new line may get in the way of your processing, here's a
contrived example:

my $foo = 'some text to append to line\n';
open my $TESTFILE, '<', '/some/test/file') or die;
while ( defined (my $line = <TESTFILE>) ) {

# $line contains 'Some arbitrary text \n'
print $line, $foo;
}

Without a 'chomp $line' the program will print:
Some arbitrary text
some text to append to line

Instead of:
Some arbitrary text some text to append to line

as was probably intended.

HTH

richf
.



Relevant Pages

  • Re: Smarter concatenation?
    ... an Ooperation (constant time for each append, ... ts 'foo 1e5' ... limited-knowledge way to do it in APL. ... by the smart Smalltalk implementation. ...
    (comp.lang.apl)
  • Re: [9fans] acme g/$/ funny
    ... shouldn't this append "foo" after every line? ... every empty line. ... not the newline itself. ...
    (comp.os.plan9)
  • appending to a list via properties
    ... foo = better_list ... qux = better_list ... Granted, the name tail might not be the best, but you get the idea. ... Use "append" instead, preserving original list.append behavior. ...
    (comp.lang.python)
  • Floating Point Number - Wierd
    ... I am running a simple script that is reading through a text file and adding up ... floating point numbers from each line. ...
    (perl.beginners)
  • Function to append to a variable
    ... append foo bar ... append: foo baz ... What if I want to append to a variable named "var"? ...
    (comp.unix.shell)