Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Loop query

Posted by TedH (TedH), 17 September 2006
Hi, trying to match a record in a flat file db.

I manage to do that as follows:
The db: data.txt
------------------------
000210|anything
000211|something
000212|wotathing
------------------------

The ID (number) is typed into a form and sent to match script - the core of which is as follows:

Code:
## open datalist put in array
open (ORGDB,"$datalist ");
# flock(DATA, 2);
@ODB=<ORGDB>;
close (ORGDB);

# read in data
foreach $rec (@ODB){
     chomp($rec);
     ($rfn,$nam)=split(/\|/,$rec);

 if($rfn =~m/$theid/)
   {
    print "($rec)<br>\n";
   }
 else
   {
   print "No match<br>\n";
   }
}
exit;


It returns it okay but looks like this

The Record
(000210|anything)
No match
No match

or  (depends which record ID is used)

The Record
No match
(000211|something)
No match

I know I can get rid of the else and only the match returns.

But is that correct? Is the script keeping on looping behind the scenes?
(it doesn't seem to but I'm not sure)

cheers - Ted

Posted by admin (Graham Ellis), 17 September 2006
It looks fine, Ted ... the loop will teminate when all the records have been looked through and no process will be left running.

If you're only looking for one matching record and can abandon the search once you've found it, add a "last" statement after you've printed the matching record.

If your datafile might get large, don't read it all in to a list at the beginning - instead, use a loop of the form
while ($rec=<ORGDB>) {
which will cause the data to be read, and analysed as it is read.  That's different to the scrit you supplied, which read the whole file into a list then processes it item by item - great for a few lines, but not so good if you're going to end up with hundreds of thousands of lines of data.


Posted by TedH (TedH), 17 September 2006
Like this Graham?

Code:
while ($rec=<ORGDB>) {
     chomp($rec);
     ($rfn,$nam)=split(/\|/,$rec);
 if($rfn =~m/$theid/)
   {
    print "($rec)<br>\n";
   }
}
close (ORGDB);
exit;


It seems to work okay. At first I used the last statement with it and it didn't like that. Picks up the record fine.

Posted by admin (Graham Ellis), 17 September 2006
Looks fine ... don't know why it didn't work with "last" ...

Code:
while ($rec=<ORGDB>) {
($rfn,$nam)=split(/\|/,$rec);
 if($rfn =~m/$theid/)
   {
    print "($rec)<br>\n";
    last;
   }
}
close (ORGDB);
exit;


which really only makes it a bit more effieicnt - stops searching once it's matched.


Posted by TedH (TedH), 17 September 2006
on 09/17/06 at 15:04:15, Graham Ellis wrote:
Looks fine ... don't know why it didn't work with "last" ...


'cuz I put it in the wrong place  

OK now

cheers - Ted



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho