probable mistake in IDL manual [message #10382] |
Fri, 28 November 1997 00:00  |
szoonem
Messages: 16 Registered: June 1995
|
Junior Member |
|
|
Hi,
I was reading "Building IDL Applications" manual (version 5) and noticed
the procedure on p. 76 which is supposed to count the number of times that
the word "dog" appears in the string "dog cat duck rabbit dog cat dog";
------
pro Animals
animals = 'dog cat duck rabbit dog cat dog'
i = 0
cnt = 0
while (i ne -1) do begin
i = strpos(animals,'dog', i)
if (i ne -1) then cnt = cnt + 1
endwhile
print, 'Found ',cnt, "occurances of 'dog'"
end
------
When I read it, it struck me as being wrong. I tested it later and it
seems to have gone into an infinite loop (as I suspected it would). Is it
some thing VERY obvious that I am missing or is it simply wrong?
I am rather confused about this simple procedure. Any comments would be
appreciated,
- Saeid
------------------------------------------------------------ -----
| Saeid Zoonematkermani | E-Mail: szoonem@astro.sunysb.edu |
| Dept. of Physics & Astronomy | Voice: (+1) (516) 632-8237 |
| State University of New York | Fax: (+1) (516) 632-8742 |
| Stony Brook, NY 11794-3800 | |
------------------------------------------------------------ -----
| Home Page --> http://ozone.ess.sunysb.edu/ |
------------------------------------------------------------ -----
|
|
|
Re: probable mistake in IDL manual [message #10456 is a reply to message #10382] |
Tue, 02 December 1997 00:00  |
David Foster
Messages: 341 Registered: January 1996
|
Senior Member |
|
|
Saeid Zoonematkermani wrote:
>
> ------
> pro Animals
> animals = 'dog cat duck rabbit dog cat dog'
> i = 0
> cnt = 0
> while (i ne -1) do begin
> i = strpos(animals,'dog', i)
> if (i ne -1) then cnt = cnt + 1
> endwhile
> print, 'Found ',cnt, "occurances of 'dog'"
> end
> ------
>
> When I read it, it struck me as being wrong. I tested it later and it
> seems to have gone into an infinite loop (as I suspected it would). Is it
> some thing VERY obvious that I am missing or is it simply wrong?
>
> I am rather confused about this simple procedure. Any comments would be
> appreciated,
Yep, looks like an infinite loop to me. Needs the following line at
the end of the while loop:
i = i + strlen('dog') ; maybe i = i + 3 is faster ;-)
Dave
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
David S. Foster Univ. of California, San Diego
Programmer/Analyst Brain Image Analysis Laboratory
foster@bial1.ucsd.edu Department of Psychiatry
(619) 622-5892 8950 Via La Jolla Drive, Suite 2240
La Jolla, CA 92037
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
|
|
|