File Handling - Perl - TechRepublic
Question
April 18, 2007 at 01:10 AM
bonjournrk

File Handling – Perl

by bonjournrk . Updated 17 years, 8 months ago

Hi,
I have written a code to count the no. of lines, words and characters in a file. When I execute the xcript, I always get 0 lines, 0 words and charcters equal to the no. of lines.

The script is below:
#!/usr/bin/perl

$l=0;
$w=0;
$chr=0;
open (FILE,”C:/nrk/programming/perl/sample.txt”);
@arr=;
for($i=0;$i<$x;$i++) { if ($arr[$i] eq "\n" || $arr[$i] eq " " || $arr[$i] eq "!") { $w++; } if ($arr[$i] eq "\n") { $l++; } else { $chr++; } } print ("The text file has $l lines, $w words and $chr characters\n"); The contents of the file "sample.txt" is as below: hello everyone!! wish you all good luck for your future. Could anyone help? Thanks, Kali.

This discussion is locked

All Comments