We have looked at storing address book entries submitted by users. The other requirement of an address book is to be able to locate an address. Now we will look at parsing address book entries and matching a user-supplied string: a search engine.
The submission side of AB1 allowed the storage of the following data: the person's first and last name; their street number, street name, suburb, post code and state; their telephone area code and land line phone number; and, optionally, their mobile phone number and e-mail address. Each of these items was stored on its own line with one or two leading prefixes attached so that the parser could identify the nature of the data on any given line. The search system will now use this structure.
Implementing A Search System
A powerful address book would allow the user to search for keywords in all the useful fields in the address book: first name, last name, street name and so on. In order to illustrate how this would be done, the following script implements a search on last name. Since it is a long script, some of the code has been omitted.
<?
/* search for the last name stored in $query */if(isset($submit) &&
(strcmp($submit,"Search") == 0)) {
$fp = fopen("address.dat","r");
while(!feof($fp)) {
$line = fgets($fp,1024);
switch($line[0]) {
case '-':
/* end of item: was it a match? */
if($match) {
$match = 0; // reset
/* out put the data */
. . .
}
break;
case 'F':
/* save the first name, in case last name
* is a match */
$fn = $line;
break;
case 'L':
if(strcmp(substr($line,1,strlen($line) - 2,$query) == 0) {
$match = 1; // last name found
$ln = substr($line,1);
} else {
continue; // no match
}
break;
case 'A':
if($match) {
if($line[1] == '1') $a1 = substr($line,1);
if($line[1] == '2') $a2 = substr($line,1);
. . .
}
}
fclose($fp);
}
?>
&sp;&sp;When the user clicks the 'Search' button to submit the HTML form, the variable $submit is set to 'Search'. Hence, by testing if $submit is set and if it is set to 'Search', the script can hand control to the search code at the correct time, i.e., when a search has been required.
The script opens the data file and cycles through it, parsing each line to see what the data is. Each line is saved in case it is required to be output (that is, if a match is made). See the code (above) for some sample data.
Notice that it is the third case in the switch control structure that does the real work: namely, to match the search query against the last name in the file. If this succeeds, the script registers that a match is made; otherwise, the script advances to the next line of the file.
When the next line is tested by the switch (in this case, a line beginning with 'A'), the situation is more complicated. There are five lines beginning with A: A1, A2, A3, A4 and A5. As such, the script checks the second character to verify which line it is. Once this is achieved, the script tests to see if we need to save data given a last name match: if yes, the save is made; otherwise, the script continues to loop.
When the script encounters a dash ('-'), it checks to see if a match has been found. If so, the script outputs the address book data and resets the $match variable to false.
Anyone interested in extending their PHP skills should attempt to modify this code to handle on last name and first name (or any other field, for that matter).
Address Book 1 is fairly limited: searching for data can only occur on last names and searching is linear (that is, the entire file is searched from top to bottom). This is a very inefficient way to search data. In order to illustrate this, I tested AB1 with large amounts of data. When there were 200,000 addresses, searching took half a second; when there were 400,000 addresses, searching took 1 second; but, when there were 800,000 addresses, searching took over 3 seconds. Though AB1 has not been designed with such large amounts of data in mind, this highlights how poorly linear text searches perform.
Discover how SOA can create smarter outcomes for your business.
Attend and learn:
- How SOA is helping leading companies to become more agile
- Where you should be applying SOA processes in your company
- The top SOA implementation mistakes to avoid
Click here for more information.
- +
Computerworld Live Podcast #97: The Future of Enterprise Networking 25/07/2008 09:45:36
This week CW Live chats with Mark Thompson, global sales and marketing manager for HP ProCurve, on the future of the enterprise networking. Mark discusses the trends we can expect to see in the near future and how the right infrastructure can ensure your enterprise network is secure. - +
Computerworld Live Podcast #96: Security at the Edge 11/06/2008 09:22:22
CW Live speaks with Amol Mitra, HP ProCurve Director of Marketing for Asia Pacific and Japan. Today's topic: how enterprises are starting to shift away from simply controlling security via server logins, firewalls and moving to more adaptive security frameworks. - +
Data Management Edition #10: Multi-Petascale Systems 02/05/2008 09:12:33
This week we look at sustainability and the development of multicore technologies to build multi-petascale systems. - +
IT Security Edition #11: How to poison the Storm botnet 01/05/2008 08:51:55
This week CW Live presents a case study on how to poison the notorious Storm botnet . Plus we take a look at Cisco's plans for Ironport. - +
IT Security Edition #10: Cyber-battles fought and won 24/04/2008 11:09:47
Vendors bow to end user pressure to improve product security, and we take a look at the latest concepts shaping the cyber-battlefield of the future.
FrontRange Solutions launches HEAT Plus Mobile to reduce help desk costs and improve service management productivity 2008-12-02 15:15:00+11
AARNet Helps to Advance Indigenous Health 2008-12-02 12:44:00+11
Orbis selects Telstra International as its data centre partner for the UK, Europe and Middle East Region 2008-12-02 11:23:00+11
ComOps Deploys Corporate Performance Reporting Solution For Healthcare Test Manufacturer 2008-12-02 10:09:00+11
Mornington Peninsula Shire implements Objective to manage knowledge and deliver service excellence 2008-12-02 09:56:00+11
Wireless LANs: Is my enterprise at risk?
Achieve an overall understanding of the risks associated with wireless LANs. Discover their inherent properties, as well as what makes them different from wired networks. Read on to uncover a list of recently published articles on real-life breaches and incidents illustrating the need for proactive measures to mitigate wireless security risks.












