Try this instead:
#!/usr/bin/perl use strict; use warnings; my @lines = <DATA>; # Don't do this unless necessary. :) chomp( @lines ); # Removes IRSs from all lines. # Loops through array, pulls out a line, trims white space from front # and back and prints the line. foreach my $line ( @lines ) { $line = trimSpaces( $line ); print $line, "\n"; } # The regular expression in the subroutine looks for zero # or more space characters at the beginning and end of the # line and removes them. sub trimSpaces() { my $value = shift( @_ ); $value =~ s/(^\s*|\s*$)//g; return $value; } __DATA__ hi there this and that and the other thing blah blah blah one two there four five six
The 'chomp' command is doing exactly what it's supposed to. It's removing the Input Record Separator (the contents of the builtin $/ variable.) If you haven't set this explicitly, the value is dictated by your operating system. You may be thinking of the 'chop' command. However, I encourage you to use care with chop as it removes the trailing character, no matter what it is. :)
HTH,
/Larry
In reply to Re: chomp not working in array
by larryp
in thread chomp not working in array
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |