Searching Part 3
Senior Editor, TheScripts.com
I want to explain this part before I explain the &grab_data and &process_record subs.... as it is a little more complicated. Just a note, this sub procedure just sorts the data as the user defines it to be sorted. It isn't important, and is a feature that can be left out of your database... just you will have to edit the code as needed. &search_sorter is sent all the data that was pushed into the array @search_results. It also returns the result into @search_results... so this part can be skipped as long as you remove it from &search_form as well.
sub search_sorter {
my (@results) = @_;
This part reserves @results as a my variable, limiting it to sub search_sorter only, and fills it with the array we sent it when calling the sub procedure.
my(@rec);
my (%temp_rec,$eval_code);
These bits just reserve the variables strictly into &search_sorter as well.
$stop = @db_fields;
Saves in the variable $stop the amount of records in @db_fields;
foreach $result (@results){
Like all of our foreach loops, this one is re-iterating over the contents of @results.... the array we sent to it.
(@rec) = &grab_data($result);
Here is that pesky &grab_data sub again. Like I mentioned above, this will be explained later.
$eval_code ='$temp_rec{$rec[0]} = { $db_key => "$rec[0]", ';
Now here is something kind of new, though it is similar to the code we had to create in $findit for the regular expression search. Anyways, this code is just starting to make a new hash..... the contents of his hash will be all of the parsed fields, and their respective values. The key to access this hash will be its ID number in the array.
for($i=1;$i<$stop;$i++){
$eval_code .= "\$db_fields[$i] => \"\$rec[$i]\",\n";
}
This bit just starts at the array index 1 (the second one really, as we have the first one already saved as a key). It will just go through all the fields in @db_fields, and create keys accordingly for this little hash we are creating.
$eval_code .= '};';
This just finishes off the $eval_code variable by ending it with a proper ending for a hash;
eval $eval_code;
Here it is again. That eval function. How is it being used here? Same way as above, its just inserting this perl code we just made into the program. Therefore, the hash we just made is now considered part of the perl script.
}
$sort_field = $form{'sort_field'};
What field are we sorting by? Well, the &search_form specifies with radio buttons a bunch of options, all with the key name of each field as their value. This just saves the value into $sort_field for readability.
@results=();
This empties the @results array, as we have all of its contents in a bunch of hashes anyways.
foreach $field (sort {lc($a->{$sort_field}) cmp lc($b->{$sort_field})} values %temp_rec){
The actual sorting takes place here. Let me explain this bit by bit....
foreach $field # Each value being re-iterated in
# the hash %temp_rec is now referred to as $field
(sort # Just as it looks. This is part of the sort function
{
lc($a->{$sort_field}) # We don't want capitalization
# getting in the way. This compares items as lowercase form....
# just as the lc() function works.
# $a->{$sort_field} sorts it in descending order by the
# $sort_field
cmp # compares to the next
# value..... alphabetically
lc($b->{$sort_field}) # Same as the one above,
# except by $b, which means it is working in
# descending order
} # Closes the sort parameters
values %temp_rec # Shows what to sort by....
# the values of %temp_rec, which eval just made
) # Ends the foreach parameters.
Well, now that perl knows how to sort everything, time to get to work
$new_record = "";
This just resets $new_record to a null string;
for($i=0;$i<$stop;$i++){
For loop again.... time to rebuild the @results array with the proper sorted data
$field->{$db_fields[$i]} =~ s/\Q$delimeter\E/~~/og;
$field->{$db_fields[$i]} =~ s/\n/``/g;
$new_record .= "$field->{$db_fields[$i]}\|";
Now We have to make sure all of our fields are encoded again so it looks right. After that, the $new_record variables adds onto itself the values of the fields.... all in proper order. Basically it is just making the data appear again as it does in the database file
}
chop $new_record;
There is an extra delimeter here.... which we are immediately ridding ourselves of.
push @results, $new_record;
Pushes into the array @results the new values of $new_record;
}
return (@results);
returns the values of @results
}
And here is the end of sub search_sorter. I hope you enjoyed your stay! Ya, I'm horrible at jokes.
return (@search_results); }
Here is the end of sub search. The values of @search_results are returned to whatever called upon &search.
