Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scan or Do While !EOF() ?? 3

Status
Not open for further replies.

thatguy

Programmer
Aug 1, 2001
283
US
Hey there folks..

This has probably been covered, but the search is Still down, so please excuse me... :)

I have a bunch of places where I need to test every record of a table and I'm wondering which is better to use and why:

select (table)
scan all
(blah blah)
endscan

or
go top in (table)
do while !eof(table)
(blah blah)
enddo

Any opinions are appreciated. Thanks
-- michael~
 
scan...endscan is faster and better in my opinion Slighthaze = NULL
 
michael,
Because the DO WHILE ... requires a SKIP, and because the SCAN will always re-select the proper table automatically, SCAN is both safer and quicker. I agree with Slighthaze.

Rick
 
by "re-select the proper table"... do you mean the table that was initally selected at the start of the loop?
 
Correct, no matter how many other tables you may have "visited" in the SCAN loop, the table selected when the scan started will be re-selected if you forget to. This is obviously NOT the case for a DO WHILE, as it has no implied table associated with it. (In fact, I often do not even use tables in DO WHILE loops!)

Rick
 
Hi

I have a bunch of places where I need to test every record of a table

While it is easy and obvious to agree with what I has been suggested... that you want to check every record with a test...

SCAN FOR myField = whatever
blah blah..
ENDSCAN

In this exapmle.. you can see that you are not checking within the loop.. but the loop is taken only when the record passes the test. This could prove even more faster if your intention is to read thru only selected records. Rushmore optimisation can be fully utilised here. :)
ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com

 
There's one 'gotcha' that isn't quite obvious. These two loop structures compare like this:
Code:
  select (table)
  scan
     (blah blah)
  endscan

* Is equivalent to 
  
  select (table)
  goto top
  do while !eof()
     (blah blah)
    select (table)
    skip
  enddo

* However, here SCAN behaves differently:

  select Table2
  do LookForIt with "This will be found"
  * Alias is now Table1

  select Table2
  do LookForIt with "This will not be found"
  * Alias is now Table2

Procedure LookForIt( pcLookFor )
LOCAL lcOSel
  lcOSel = alias()
  select table1
  scan
    if ( pcLookFor $ Field1 ) ;
       or (pcLookFor $ Field2 )
      * Do whatever else you must
      select (lcOSel)
      RETURN && !!!! Table1 will automatically be selected
             &&  here because we are leaving the SCAN.
    endif
  endscan
  select (lcOSel)
  RETURN

So it's clear that the two structures are NOT exactly the same... it's impossible to completely duplicate the SCAN loop with a DO WHILE, and you cannot leave a SCAN loop (even with RETURN) without changing the alias back to what it was when SCAN was started.
 
"Scan for " takes advantage of simple indexes I have used this in a bank which had millions of records but wanted to scan the particular customer.

To find say 10 records within 5M records took milliseconds, but remember you must only use SIMPLE indexes

Rich
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top