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: File creation issue

2004-12-26 Thread Conrad Schilbe
Adam Butler wrote:
I forgot to mention that $file is set via a form.  On first run of the
script, it prints a form to the browser asking for a file name, which you
enter, and then it's submited and sent to the script.  The way I understood
it, when open tries to open a file that doesn't exist, it creates it.
Thanks,
 

The source of $file doesn't really matter, but I hope you are 
sanitizing that input!

Open(GAMELOG, $file);
   @entries = GAMELOG;
close(GAMELOG);
 

This snippet will not create the file if it doesn't exist. As mentioned 
before, you can open a file for read or write. When opening for write, 
the file will be created if it does not exist but what would be the 
point of this if you are only reading the file?

If this is actually what you want, add a test routine that creates the 
file if it doesn't exist:

if (!( -f $file )) {
   # create the file
   open(FH,  $file);
   close(FH);
}
open(GAMELOG, $file) || die $!\n;
   @entries = GAMELOG;
close(GAMELOG);
If you want to just test for the file instead of creating it too:
if ( -f $file ) {
   open(GAMELOG, $file) || die $!\n;   # you may still die with 
permission problems even though it exists.
  @entries = GAMELOG;
   close(GAMELOG);
}

-- cs
You've had your answer. in your script 'Open' means nothing and the
file won't be created even if you use 'open'.
  #!/usr/bin/perl
  chdir /tmp;
  $log = game.log;
  open LOG, $log or die $!; # --- !
  print LOG success !;
  close LOG;
  open LOG, $log;
  for (LOG) { print };
Make it a rule NEVER to open a filehandle without testing.
JD
   

 




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.