Home > Archive > PERL Beginners > August 2005 > A small problem
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]
|
|
| Alok Nath 2005-08-22, 7:55 am |
| Hi Guys,
I have this script which reads this text file and splits each
line
based on comma operator.The strange thing is it is removing the
last
character from the last word in the last line.So it is not
displayed completely.
Can anybody tell me why its so ?
Any help will be greatly appreciated ?
Thanx,
Alok.
#!/usr/bin/perl
$testFile = "TestTemplates.txt" ;
open(DATA, $testFile) || die "Unable to open file" ;
@test = <DATA> ;
foreach $val (@test)
{
chop ($val) ;
($val1, $val2) = split(/\,/,$val) ;
print "val1 = $val1 \n" ;
print "val2 = $val2 \n" ;
}
close (DATA) ;
exit 0 ;
TestRemplates.txt
iteration8H.SampleServiceDesign_test.xml,create
iteration8H.SampleServiceDesign_test.png,test
iteration8H.SampleServiceDesign_test.xml,delete123
Output :
val2 = create
val1 = iteration8H.SampleServiceDesign_test.png
val2 = test
val1 = iteration8H.SampleServiceDesign_test.xml
val2 = delete12esign_test.xml,delete12
| |
| John W. Krahn 2005-08-22, 7:56 am |
| Nath, Alok (STSD) wrote:
> Hi Guys,
Hello,
> I have this script which reads this text file and splits each line
> based on comma operator.The strange thing is it is removing the last
> character from the last word in the last line.So it is not displayed completely.
> Can anybody tell me why its so ?
chop() removes the last character from a line no matter what it is so if the
last line in the file does not end with a newline this will happen. Use
chomp() instead.
John
--
use Perl;
program
fulfillment
| |
| Xavier Noria 2005-08-22, 7:56 am |
| On Aug 22, 2005, at 13:26, Nath, Alok (STSD) wrote:
> Hi Guys,
> I have this script which reads this text file and splits each
> line
> based on comma operator.The strange thing is it is removing the
> last
> character from the last word in the last line.So it is not
> displayed completely.
> Can anybody tell me why its so ?
My bet is that the last line has no newline character at the end.
Replace the call to chop() which unconditionally removes the last
character, with a call to chomp(), which removes the newline[*] if it
finds it.
BTW, there is no need to escape the comma in the regexp.
-- fxn
[*] To be precise removes a trailing $/, which defaults to \n.
|
|
|
|
|