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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database searching.... 1

Status
Not open for further replies.

Vesku

Programmer
Jun 7, 2000
19
0
0
FI
Hi !<br><br>Is there simple way to search from database ?<br><br>This is what my database looks:<br><br><b>1¦data1¦data2¦data3¦data4<br>2¦data1¦data2¦data3¦data4<br>3¦data1¦data2¦data3¦data4</b><br><br>How can i make search-script to that kind database ?<br><br><br><br>
 
Well... when you parse your data, create an array where the size of the array matches the number of rows. Then, in each index of that array, create an array where the size of the array matches the number of columns. So if I wanted to access the data in row 2, column 3, I'd access the outer array with index 2, inner array index 3. Or for searching, I could go through each index and see if any of the indexes for the inner arrays contained my data.<br><br>That's not a smart database, though... and that's only one way to do it, there are plenty of others (what's the Perl motto? &quot;There's more than one way to do it&quot;?).<br><br>exactly what kind of data will you be populating your database with? This would help to determine the optimum way of dealing with your data in Perl. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Try this:<br><FONT FACE=monospace><br>#!/usr/bin/perl<br><br><i>Put some code in here to somehow get <br>the search string we are looking for into<br>the variable $Search_String</i><br><br># Read the contents of the database file into<br># an array.<br>open(DATABASE, &quot;&lt;<i>/path/to/my/database/file</i>&quot;);<br>@DB_Array = &lt;DATABASE&gt;;<br>close(DATABASE);<br><br># Now use the grep function to look for my<br># search string.<br>@Results = grep(/$Search_String/, @DB_Array);<br><br># output the results.<br>print &quot;I found the following matches:\n\n&quot;;<br><br>foreach $Item (@Results) {<br>&nbsp;&nbsp;&nbsp;&nbsp;print $Item;<br>}<br></font><br>Of course, if your database file is large, then the memory requirements for @DB_Array will also be large...&nbsp;&nbsp;I leave it as an exercise for the reader to format the output ;^)<br><br>Hope this helps. <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
...One question about database (again!!)...<br><br>Andy, your script was great!<br><br>If i use lowercase letters that script does not find uppercase letters.<br><br>Ie.<br><br>James¦Smith¦Somestreet<br><br>If i use keyword &quot;james&quot;, nothing found...<br><br>Please help!<br><br>Thanx!
 
I'm glad it's working for you :)<br><br>To make the search case-insensitive, you want to make the regular expression case-insensitive.&nbsp;&nbsp;This is as easy as putting &quot;i&quot; at the end of the regex.&nbsp;&nbsp;For example:<br><FONT FACE=monospace><br>@Results = grep(/$Search_String/i, @DB_Array);<br></font><br>Put the &quot;i&quot; into your script, and that should work OK. <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
I had a thought after my last post - what if, in the future, you want to do an &quot;and&quot; type search on multiple key words?&nbsp;&nbsp;My first attempt was to build up a string of greps, then eval the string:<br><FONT FACE=monospace><br>@Array = (&quot;Andy¦moretext¦windows¦nothanks&quot;, &quot;andy¦sometext¦perl¦unix&quot;, &quot;Fred¦othertext¦windows¦yesplease&quot;);<br><br>@To_Find = (&quot;Andy&quot;, &quot;Unix&quot;);<br><br># Set up a string of greps to do an &quot;and&quot; type search on multiple keywords.<br>foreach $Word (@To_Find) {<br>&nbsp;&nbsp;&nbsp;&nbsp;$Search_String = $Search_String . &quot;grep(/&quot; . <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$Word . &quot;/i, &quot;;<br>}<br><br># Terminate the search string.<br>$Search_String .= &quot;\@Array&quot; . &quot;)&quot; x ($#To_Find + 1) . &quot;;&quot;;<br><br>print $Search_String, &quot;\n&quot;;<br><br># Evaluate the search string, putting the results into @Found.<br>@Found = eval($Search_String);<br><br>print @Found;<br></font><br>Then I realised that a slighter easier to understand method might be to just loop around each keyword and grep for it:<br><FONT FACE=monospace><br>@Array = (&quot;Andy¦moretext¦windows¦nothanks&quot;, &quot;andy¦sometext¦perl¦unix&quot;, &quot;Fred¦othertext¦windows¦yesplease&quot;);<br><br>@To_Find = (&quot;Andy&quot;, &quot;Unix&quot;);<br><br># Take a copy of @Array that will be reduced until we have <br># through each keyword.<br>@Search_This = @Array;<br><br># Now loop around each keyword.<br>foreach $Keyword (@To_Find) {<br>&nbsp;&nbsp;&nbsp;&nbsp;# Each time around the loop, @Search_This will only<br>&nbsp;&nbsp;&nbsp;&nbsp;# contain items that contain the current key word.<br>&nbsp;&nbsp;&nbsp;&nbsp;@Search_This = grep(/$Keyword/i, @Search_This);<br>}<br><br>print @Search_This;<br></font><br>And then I got to wondering how I would do an &quot;OR&quot; type search and came up with:<br><FONT FACE=monospace><br>@Array = (&quot;Andy¦moretext¦windows¦nothanks&quot;, &quot;john¦sometext¦perl¦unix&quot;, &quot;Fred¦othertext¦windows¦yesplease&quot;);<br><br>@To_Find = (&quot;Andy&quot;, &quot;Unix&quot;);<br><br># Join the keywords together into a single pipe separated<br># string.&nbsp;&nbsp;Grep will treat this as a &quot;or&quot; type search.<br># Note that the &quot;¦&quot; is not related to the &quot;¦&quot; characters <br># in the original string.&nbsp;&nbsp;It is a regex &quot;or&quot; operator.<br>$Keyword_List = join(&quot;¦&quot;, @To_Find);<br><br>@Found = grep(/$Keyword_List/i, @Array);<br><br>print @Found;<br></font><br><br>Anyway, it livened up a boring lunch time, and I hope it helps someone :) <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top