×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Database searching....

Database searching....

Database searching....

(OP)
Hi !

Is there simple way to search from database ?

This is what my database looks:

1¦data1¦data2¦data3¦data4
2¦data1¦data2¦data3¦data4
3¦data1¦data2¦data3¦data4


How can i make search-script to that kind database ?



RE: Database searching....

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.

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? "There's more than one way to do it"?).

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.

Liam Morley
lmorley@wpi.edu
] :: imotic :: website :: [
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."

RE: Database searching....

Try this:

#!/usr/bin/perl

Put some code in here to somehow get
the search string we are looking for into
the variable $Search_String


# Read the contents of the database file into
# an array.
open(DATABASE, "</path/to/my/database/file");
@DB_Array = <DATABASE>;
close(DATABASE);

# Now use the grep function to look for my
# search string.
@Results = grep(/$Search_String/, @DB_Array);

# output the results.
print "I found the following matches:\n\n";

foreach $Item (@Results) {
    print $Item;
}

Of course, if your database file is large, then the memory requirements for @DB_Array will also be large...  I leave it as an exercise for the reader to format the output ;^)

Hope this helps.




--
0 1 - Just my two bits

RE: Database searching....

(OP)
Thank you AndyBo !!!!

RE: Database searching....

(OP)
...One question about database (again!!)...

Andy, your script was great!

If i use lowercase letters that script does not find uppercase letters.

Ie.

James¦Smith¦Somestreet

If i use keyword "james", nothing found...

Please help!

Thanx!

RE: Database searching....

I'm glad it's working for you :)

To make the search case-insensitive, you want to make the regular expression case-insensitive.  This is as easy as putting "i" at the end of the regex.  For example:

@Results = grep(/$Search_String/i, @DB_Array);

Put the "i" into your script, and that should work OK.




--
0 1 - Just my two bits

RE: Database searching....

I had a thought after my last post - what if, in the future, you want to do an "and" type search on multiple key words?  My first attempt was to build up a string of greps, then eval the string:

@Array = ("Andy¦moretext¦windows¦nothanks", "andy¦sometext¦perl¦unix", "Fred¦othertext¦windows¦yesplease");

@To_Find = ("Andy", "Unix");

# Set up a string of greps to do an "and" type search on multiple keywords.
foreach $Word (@To_Find) {
    $Search_String = $Search_String . "grep(/" .
        $Word . "/i, ";
}

# Terminate the search string.
$Search_String .= "\@Array" . ")" x ($#To_Find + 1) . ";";

print $Search_String, "\n";

# Evaluate the search string, putting the results into @Found.
@Found = eval($Search_String);

print @Found;

Then I realised that a slighter easier to understand method might be to just loop around each keyword and grep for it:

@Array = ("Andy¦moretext¦windows¦nothanks", "andy¦sometext¦perl¦unix", "Fred¦othertext¦windows¦yesplease");

@To_Find = ("Andy", "Unix");

# Take a copy of @Array that will be reduced until we have
# through each keyword.
@Search_This = @Array;

# Now loop around each keyword.
foreach $Keyword (@To_Find) {
    # Each time around the loop, @Search_This will only
    # contain items that contain the current key word.
    @Search_This = grep(/$Keyword/i, @Search_This);
}

print @Search_This;

And then I got to wondering how I would do an "OR" type search and came up with:

@Array = ("Andy¦moretext¦windows¦nothanks", "john¦sometext¦perl¦unix", "Fred¦othertext¦windows¦yesplease");

@To_Find = ("Andy", "Unix");

# Join the keywords together into a single pipe separated
# string.  Grep will treat this as a "or" type search.
# Note that the "¦" is not related to the "¦" characters
# in the original string.  It is a regex "or" operator.
$Keyword_List = join("¦", @To_Find);

@Found = grep(/$Keyword_List/i, @Array);

print @Found;


Anyway, it livened up a boring lunch time, and I hope it helps someone :)




--
0 1 - Just my two bits

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close