Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Why does this not print the filenames ?

Reply
Thread Tools

Why does this not print the filenames ?

 
 
elhombre
Guest
Posts: n/a
 
      03-13-2009
I'm sure this is a typical newbie error but I cannot see why this prints

FILE wordcount is 17 when it should print c:\original.txt word count is 17.

If I try

print $_ . " word count is " . $wordCount . "\n";

it complains that $_ is unitialised and I cannot see why.

Any ideas ?


foreach (@ARGV) {
open(FILE, $_) or die "File $_ does not exist";
while (<FILE>) {
my @words = split;
foreach my $word (@words) {
$wordCount++;
}
}
print FILE . " word count is " . $wordCount . "\n";
close FILE;
$wordCount = 0;
}

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      03-13-2009
"elhombre" <> wrote:
>I'm sure this is a typical newbie error but I cannot see why this prints
>
>FILE wordcount is 17 when it should print c:\original.txt word count is 17.
>
>If I try
>
>print $_ . " word count is " . $wordCount . "\n";
>
>it complains that $_ is unitialised and I cannot see why.
>
>Any ideas ?
>
>
>foreach (@ARGV) {
> open(FILE, $_) or die "File $_ does not exist";
> while (<FILE>) {
> my @words = split;
> foreach my $word (@words) {
> $wordCount++;


The whole while() loop can better be written as
$wordcount = @words;
because an array used in scalar context returns the number of its
elements.No need to manually count them.

And that will also solve the warning message, because you are using $_
for two conflicting purposes: to iterate over @ARGV and to iterate over
@words.

> }
> }
> print FILE . " word count is " . $wordCount . "\n";


You aren't printing $_, you are printing FILE.

> close FILE;
> $wordCount = 0;
>}


jue
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      03-13-2009
Jürgen Exner <> wrote:
>"elhombre" <> wrote:


>>foreach (@ARGV) {
>> open(FILE, $_) or die "File $_ does not exist";
>> while (<FILE>) {
>> my @words = split;
>> foreach my $word (@words) {
>> $wordCount++;

>
>The whole while() loop can better be written as


Daaah!
Note to self: Before posting turn on brain.

Make that the whole foreach() loop could be written as
$wordCount += @words;

>because an array used in scalar context returns the number of its
>elements.No need to manually count them.


jue
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-13-2009
elhombre wrote:
> I'm sure this is a typical newbie error but I cannot see why this prints
>
> FILE wordcount is 17 when it should print c:\original.txt word count is
> 17.


Well, I don't understand how you got that far with the code below. When
I tried it, Perl complained with the error message "Modification of a
read-only value attempted".

Only after having changed

> while (<FILE>) {
> my @words = split;


to

while ( my $line = <FILE> ) {
my @words = split ' ', $line;

the script produces the output you say.

> If I try
>
> print $_ . " word count is " . $wordCount . "\n";
>
> it complains that $_ is unitialised and I cannot see why.


With the above change, it does not complain.

> Any ideas ?


You are trying to alias $_ to both a file name and a line. In cases when
you have an outer and an inner loop, you need to be specific in at least
one of them.

> foreach (@ARGV) {
> open(FILE, $_) or die "File $_ does not exist";
> while (<FILE>) {
> my @words = split;
> foreach my $word (@words) {
> $wordCount++;
> }
> }
> print FILE . " word count is " . $wordCount . "\n";
> close FILE;
> $wordCount = 0;
> }


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
elhombre
Guest
Posts: n/a
 
      03-13-2009

"Jürgen Exner" <> wrote in message
news:...
> "elhombre" <> wrote:
> The whole while() loop can better be written as
> $wordcount = @words;
> because an array used in scalar context returns the number of its
> elements.No need to manually count them.
>
> And that will also solve the warning message, because you are using $_
> for two conflicting purposes: to iterate over @ARGV and to iterate over
> @words.
>
>> }
>> }
>> print FILE . " word count is " . $wordCount . "\n";

>
> You aren't printing $_, you are printing FILE.
>
>> close FILE;
>> $wordCount = 0;
>>}

>
> jue


Thanks Jue ! So simplifying gives me (I am going to rename FILE to FH so as
to hopefully avoid confusion

foreach (@ARGV) {
open(FH, $_) or die "File $_ does not exist";
while (<FH>) {
@words = split;
$wordCount = @words;
}
print FH . " word count is " . $wordCount . "\n";
close FH;
}

Which gives the output

FH word count is 18

when what I am trying to get is

c:\original.txt word count is 18.

Which indicates of course that the filehandle FH is not the actual name of
the file being opened ? How do I get it to print "c:\original.txt" ?

Thanks again.

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-13-2009
Jürgen Exner wrote:
> "elhombre" <> wrote:
>>
>> foreach (@ARGV) {
>> open(FILE, $_) or die "File $_ does not exist";
>> while (<FILE>) {
>> my @words = split;
>> foreach my $word (@words) {
>> $wordCount++;


<corrected> (in accordance with your next message)
> The whole foreach() loop could be written as
> $wordCount += @words;

</corrected>

> because an array used in scalar context returns the number of its
> elements.No need to manually count them.
>
> And that will also solve the warning message, because you are using $_
> for two conflicting purposes: to iterate over @ARGV and to iterate over
> @words.


No, he is using $_ to iterate over @ARGV and to iterate over <FILE>, so
that conflict still remains. (See my post.)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Michael Austin
Guest
Posts: n/a
 
      03-13-2009
elhombre wrote:
> I'm sure this is a typical newbie error but I cannot see why this prints
>
> FILE wordcount is 17 when it should print c:\original.txt word count is
> 17.
>
> If I try
>
> print $_ . " word count is " . $wordCount . "\n";
>
> it complains that $_ is unitialised and I cannot see why.
>
> Any ideas ?
>
>
> foreach (@ARGV) {
> open(FILE, $_) or die "File $_ does not exist";
> while (<FILE>) {
> my @words = split;
> foreach my $word (@words) {
> $wordCount++;
> }
> }
> print FILE . " word count is " . $wordCount . "\n";
> close FILE;
> $wordCount = 0;
> }



made a few mods - this works better (see comments below...

use strict;

foreach (@ARGV) {
my $file = $_;
my ($wordCount,$word);

open(FILE, $file) or die "File $file does not exist";
while (<FILE>) {
my @words = split;
foreach my $word (@words) {
$wordCount++;
}
}
print $file . " word count is " . $wordCount . "\n";
close FILE;
}


Try uncommenting the $file def and replace $_ with $file and see if you
get any different results.

perl g.pl login.com login.com y.com version.com
word count is 370
word count is 370
word count is 14
word count is 63

VS.

perl g.pl login.com login.com y.com version.com
login.com word count is 370
login.com word count is 370
y.com word count is 14
version.com word count is 63
 
Reply With Quote
 
elhombre
Guest
Posts: n/a
 
      03-13-2009
"Michael Austin" <> wrote in message
news:k8jul.18590$...

> made a few mods - this works better (see comments below...
>
> use strict;
>
> foreach (@ARGV) {
> my $file = $_;
> my ($wordCount,$word);
>
> open(FILE, $file) or die "File $file does not exist";
> while (<FILE>) {
> my @words = split;
> foreach my $word (@words) {
> $wordCount++;
> }
> }
> print $file . " word count is " . $wordCount . "\n";
> close FILE;
> }
>
>

Thank you Michael ! I've spent 3 hours at this and it was something so
simple. Thanks as well to Jue and Gunter, your help is greatly appreciated
and very valuable.

 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      03-13-2009
elhombre <> wrote:

> I'm sure this is a typical newbie error but I cannot see why this prints
>
> FILE wordcount is 17 when it should print c:\original.txt word count is 17.


> open(FILE, $_) or die "File $_ does not exist";


> print FILE . " word count is " . $wordCount . "\n";



There is no way to get a filename from a filehandle.

There may, in fact, be no filename associated with a filehandle:

open FILE, 'date|' or die "problem running date program $!";

What "filename" should FILE have now?


If you want to use the filename later, save it into a variable.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-13-2009
Gunnar Hjalmarsson wrote:
> elhombre wrote:
>> I'm sure this is a typical newbie error but I cannot see why this prints
>>
>> FILE wordcount is 17 when it should print c:\original.txt word count
>> is 17.

>
> Well, I don't understand how you got that far with the code below. When
> I tried it, Perl complained with the error message "Modification of a
> read-only value attempted".
>
> Only after having changed
>
>> while (<FILE>) {
>> my @words = split;

>
> to
>
> while ( my $line = <FILE> ) {
> my @words = split ' ', $line;
>
> the script produces the output you say.


After having corrected Jürgen, I have to correct myself as well. The
above change is advisable; not for the reason I mentioned above, but to
prevent the warning message when $_ is printed after the while loop.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
FAQ Topic - Why does framename.print() not print the correct frame in IE? FAQ server Javascript 1 04-19-2007 02:37 AM
Why does VS not show build error filenames in ASP.NET 2.0 projects? Michael Bray ASP .Net 5 03-19-2007 02:10 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
problem with filenames, Filenames and FILENAMES B.J. HTML 4 04-23-2005 08:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57