Re: Reading in a File

2004-12-27 Thread Conrad Schilbe
Doug McNutt wrote:
At 22:44 -0500 12/25/04, Lola Lee wrote:
 

Nothing happens.  This lesson that I'm working on is working from the premise that people are using a Windows Perl installation.
   

Watch out for line ends in the source file that is being counted. Perl 
probably doesn't care because the return-linefeed pair from Windoze still 
contains the single linefeed that UNIX expects. But I have been fooled, 
especially with Unicode's two new code points representing line ends.
 

Also, I believe that the snippet suggested could stop if there is a 
blank line in the file! Although, it would still contain a linefeed, I 
rarely trust interpretation of such.
   while (defined($line = ))

I prefer this method:
   while ($line = ) {
  next if ($line =~ /^\s*$/);
   }
-- cs
  


Re: Reading in a File

2004-12-26 Thread Marco Baroni
 while (defined($line = ))

This line looks good in any standard setup (e.g., using perl on the
terminal in osx). What is your setup? Are you sure that the problem
is not elsewhere?

Regards,

Marco


Re: Reading in a File

2004-12-26 Thread John Delacour
At 10:44 pm -0500 25/12/04, Lola Lee wrote:
This script has you count words in a file.  The line where one is 
supposed to read in the file being counted is like so:

while (defined($line = ))
However, when I type this in: perl countwords.pl history.txt
Nothing happens.

The line you quote simply puts each line of a putative file into the 
scalar variable $line.  What happens will depend on how you deal 
with $line and where you print your results.

Supposing that countwords.pl is a script something like the one below 
and that history.txt is a file in the same directory (say your home 
directory), then you will get output in the terminal as below when 
you run your command.  I personally would substitute for your line:

  foreach $line ()
which does the same thing and, to me at least, is simpler and clearer.
###
#!/usr/bin/perl
foreach (@ARGV) {
  while (defined ($line = )) {
@words = split /\s+/, $line;
$wordcount = @words;
print $wordcount, ;
$total += $wordcount;
  }
  print \n$_Total: $total \words\\n;
}
###
_TERMINAL_
eremita:~ jd$ cd
eremita:~ jd$ perl countwords.pl histoire.html
7, 2, 4, 2, 4, 6, 5, 4, 2, 6, 2, 0, 1, 1, 0, 3, 1, 1,
histoire.htmlTotal: 51 words
eremita:~ jd$

.


Re: Reading in a File

2004-12-26 Thread Lola Lee
Marco Baroni said the following on 12/26/04 4:20 AM:
This line looks good in any standard setup (e.g., using perl on the
terminal in osx). What is your setup? Are you sure that the problem
is not elsewhere?
Hmm . . . looks like the script that John Delacour provided works.  So 
something's gotta be wrong with the one I typed in.  Going to have to go 
back and compare it with my printout of the lesson to make sure I didn't 
leavy anything out.

--
Lola - mailto:[EMAIL PROTECTED]
http://www.lolajl.net | Blog at http://www.lolajl.net/blog/
Terrorism delenda est! (Terrorism must be destroyed utterly!)
I'm in Bowie, MD, USA, halfway between DC and Annapolis.


Re: Reading in a File

2004-12-26 Thread Doug McNutt
At 22:44 -0500 12/25/04, Lola Lee wrote:
Nothing happens.  This lesson that I'm working on is working from the premise 
that people are using a Windows Perl installation.

Watch out for line ends in the source file that is being counted. Perl probably 
doesn't care because the return-linefeed pair from Windoze still contains the 
single linefeed that UNIX expects. But I have been fooled, especially with 
Unicode's two new code points representing line ends.

-- 

--  Halloween  == Oct 31 == Dec 25 == Christmas  --


Re: Reading in a File

2004-12-26 Thread Ken Williams
On Dec 26, 2004, at 4:34 AM, John Delacour wrote:
At 10:44 pm -0500 25/12/04, Lola Lee wrote:
This script has you count words in a file.  The line where one is 
supposed to read in the file being counted is like so:

while (defined($line = ))
However, when I type this in: perl countwords.pl history.txt
Nothing happens.

The line you quote simply puts each line of a putative file into the 
scalar variable $line.  What happens will depend on how you deal 
with $line and where you print your results.

Supposing that countwords.pl is a script something like the one below 
and that history.txt is a file in the same directory (say your home 
directory), then you will get output in the terminal as below when you 
run your command.  I personally would substitute for your line:

  foreach $line ()
which does the same thing and, to me at least, is simpler and clearer.
It doesn't quite do the same thing.  The 'while' loop will read one 
line, then process it, then read the next line, then process it, and so 
on.  The 'foreach' loop will read all the lines into memory, then 
process each of them one by one.  So if it's a large file, you'll have 
the whole thing in memory at once.

The simplest, if you like using the default variable $_, is this:
  while () {
...
  }
which is shorthand for:
  while (defined($_ = )) {
...
  }
 -Ken


Re: Reading in a File

2004-12-26 Thread John Horner
Short answer: show us the whole script, and tell us about the file 
history.txt.

The most likely explanation for why do something for each line of file 
x would do nothing is that there are no lines in file x, right? That 
is, it will do something three times for a three-line file, and zero 
times for a zero-line file, or a file that doesn't exist. It's not 
failing, it's doing what it's told to do.

So where is history.txt, and what does it contain? Is it in the same 
folder as countwords.pl for a start?

jh
On 26/12/2004, at 2:44 PM, Lola Lee wrote:
I'm taking a Perl class and and I'm working on comprehending this 
script in one of the lessons.  This script has you count words in a 
file.  The line where one is supposed to read in the file being 
counted is like so:

while (defined($line = ))
However, when I type this in: perl countwords.pl history.txt
Nothing happens.  This lesson that I'm working on is working from the 
premise that people are using a Windows Perl installation.  So it 
looks like I need to change the line above so that it will work in my 
setup. What would be a good way to change this line?

--
Lola - mailto:[EMAIL PROTECTED]
http://www.lolajl.net | Blog at http://www.lolajl.net/blog/
Terrorism delenda est! (Terrorism must be destroyed utterly!)
I'm in Bowie, MD, USA, halfway between DC and Annapolis.



Reading in a File

2004-12-25 Thread Lola Lee
I'm taking a Perl class and and I'm working on comprehending this script 
in one of the lessons.  This script has you count words in a file.  The 
line where one is supposed to read in the file being counted is like so:

while (defined($line = ))
However, when I type this in: perl countwords.pl history.txt
Nothing happens.  This lesson that I'm working on is working from the 
premise that people are using a Windows Perl installation.  So it looks 
like I need to change the line above so that it will work in my setup. 
What would be a good way to change this line?

--
Lola - mailto:[EMAIL PROTECTED]
http://www.lolajl.net | Blog at http://www.lolajl.net/blog/
Terrorism delenda est! (Terrorism must be destroyed utterly!)
I'm in Bowie, MD, USA, halfway between DC and Annapolis.