I am suppose to make a perl script that counts the number of words in a text file and then creates a file called output file and writes "there are [number] words in input.txt". Yeah this is suppose to be easy but i suck balls so yeah. this is what i have, and it doesnt work and stuff.
Code:#!C:\perl-5.10.0\bin\perl.exe $wordCount = 0; open(input, "input.txt") || die("No file:\n$!"); open(output, ">output.txt") || die("No file:\n$!"); $i = 0; while($line) { $line = <input>; @text[$i] = $line; @words = split(' ',@text[$i]); print "$i"; for($i = 0 ; $i < scalar(@words) ; $i++) { $wordCount++; print "$i"; } print $wordCount; $i++; } select(output); print "There are $wordCount words in input.txt"; #close files
Last edited by WingedPanther; 02-17-2009 at 08:27 AM. Reason: add code tags (the # button)
what output are you getting?
Your code displays a near complete lack of understanding of how to write a program to solve the requirements. Its not even close. You can google for:
perl count words
and find many solutions to this class/course assignment
Perl File Handling: open, read, write and close files
Won't tell you how to do it, but should point you in the right direction in terms of reading from files.
Programming is an art form. Everyone can program, but few can do it right.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks