Hi Doug,

On Wednesday 31 Mar 2010 07:55:25 Doug Cacialli wrote:
> I'm completely baffled by this and not entirely sure where to start.
> 
> I have a plain text  file, testfile.txt, which contains a single line:
> 
> Very truly yours,
> 
> It is written exactly how you see it above, with a newline at the end.
> 
> I'm trying to write a script that will determine the number of words
> in the file.  A snippet of what I have thus far is the following:
> 
> my $fh = new IO::File("$lvl2path/$filestng", "r") ||
>       die ("Can't open .txt file named at $lvl2path.  Exiting program.
\n\n");
> while (my $line = $fh->getline())
> {
>       my @words = split /\s+/, $line;
>       my %count = ();
>       $count{$line} += @words;
>       print "$line";
>       print "The line above has " . scalar @words . " occurrences of
> something.\n"; }
> $fh->close();
> 
> That outputs the following:
> 
> V e r y  t r u l y  y o u r s ,
> The line above has 3 occurrences of something.
> 

I cannot reproduce it here ( perl-5.10.1-8mdv2010.1 on Mandriva Linux Cooker). 
With "testfile.txt" as:

{{{
Very truly yours,
}}}

and io-file.pl as:

{{{
#!/usr/bin/perl

use strict;
use warnings;

use IO::File;

my $fh = new IO::File("testfile.txt", "r") ||
        die ("Can't open .txt file named at test.txt.  Exiting program.\n\n");
while (my $line = $fh->getline())
{
        my @words = split /\s+/, $line;
        my %count = ();
        $count{$line} += @words;
        print "$line";
        print "The line above has " . scalar @words . " occurrences of 
something.\n";
}
$fh->close();
}}}

I had to complete some of the missing code. I'm getting:

{{{
shlomi:~$ perl io-file.pl
Very truly yours,
The line above has 3 occurrences of something.
shlomi:~$ 
}}}

Which appears to be fine. In order to help you, please give us a:

1. Self-contained reproduction recipe. You may wish to prepare a file in the 
current directory inside the script and then open it for reading.

2. Your system's specs: operating system, distribution, distribution version, 
perl version, and other relevant information.

Good luck!

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap

Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to