Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Newbie asking for help
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Christopher Browne  
View profile  
 More options Jun 23 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: cbbro...@news.hex.net (Christopher Browne)
Date: 2000/06/23
Subject: Re: Newbie asking for help
Centuries ago, Nostradamus foresaw a time when The Glauber would say:

>In article <8iti9v$d1...@nnrp1.deja.com>, glauber wrote:
>[...]
>> ;; count number of lines in file key.html
>> (with-open-file (ifile "key.html" :direction :input)
>>                 (setf nlines 0)
>>                 (loop
>>                   (if (read-line ifile nil)
>>                     (setf nlines (+ 1 nlines))
>>                     (return nlines))))

>> It doesn't work (it just hangs, so i think it's stuck in the loop). I
>[...]

>OK. Using TRACE, i found that read-line wasn't returning nil, but
>something like #<END-OF-FILE>. So i added the third optional parameter,
>the one that tells it what to return at end of file. This made me do
>the (IF) backwards:

#<END-OF-FILE> may be "pretty cool," but certainly isn't conformant
with the standard.

>;; count lines
>(with-open-file (ifile "key.html" :direction :input)
>                (setf nlines 0)
>                (loop
>                  (if (eql 'eof (read-line ifile nil 'eof))
>                    (return (print nlines))
>                    (setf nlines (+ 1 nlines)))))

>And it works...

>And, of course, this works too:

>(with-open-file (ifile "key.html" :direction :input)
>                (setf nlines 0)
>                (loop
>                  (if (read-line ifile nil nil)
>                    (setf nlines (+ 1 nlines))
>                    (return (print nlines)))))

>So, i plod along, undaunted...
>I'm pretty sure this is an ugly program, and i appreciate suggestions.

I somewhat like the following:
(with-open-file
 (ifile "c:/home/html/lynx-boo.htm" :direction :input)
 (loop
  for line = (read-line ifile nil 'eof)
  for lines = 0 then (+ lines 1)   ;;; Line count...
  until (eql line 'eof)
  finally (return lines)))

The use of "for" clauses has the merit of eliminating the extra
control structures inside the loop.
--
cbbro...@acm.org - <http://www.ntlug.org/~cbbrowne/lsf.html>
Rules of the Evil Overlord #132. "Before appointing someone as my
trusted lieutenant, I will conduct a thorough background investigation
and security clearance. <http://www.eviloverlord.com/>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.