in reply to How to place Search Results!!!

Sort the results array. The most fun way to do it is something like:

#!/usr/bin/perl -w use strict; use vars qw(@results); @results = ("Bean Burrito|0.69|5", "Seven-Layer Burrito|1.39|8", "Pintos -n- cheese|0.59|0", ); sub numerically_by_item { my($which)=@_; return sub { (split(/\|/,$a))[$which] <=> (split(/\|/,$b))[$which] } } sub alpha_by_item { my($which)=@_; return sub { (split(/\|/,$a))[$which] cmp (split(/\|/,$b))[$which] } } my $sortby; print "Sorted by price\n"; $sortby = numerically_by_item(1); print join("\n",sort $sortby @results); print "\n\n"; print "Sorted by name\n"; $sortby = alpha_by_item(0); print join("\n",sort $sortby @results); print "\n\n"; print "Sorted by messiness\n"; $sortby = numerically_by_item(2); print join("\n",sort $sortby @results); print "\n\n";

Replies are listed 'Best First'.
Re: Re: How to place Search Results!!!
by ACJavascript (Acolyte) on Aug 15, 2003 at 20:01 UTC
    Hey sgifford, The code you presented gave me some errors. But i think I know were your going with it.

    I was thinking. Would this work.

    1. User Types in string.
    2. The string is checked searchs through the database. 3. Any results are then pushed into a Hash or Array 4. Then the array (hash) is sorted by least price. 5. then printed out.

    Would that work??

    A*C

      That's odd that it gave you errors. I just re-ran it, and it worked fine.

      Anyways, yes, I think what you're thinking will work.

      If your flat-file database gets very large, you'll want to look at using a db or dbm database, or maybe SQL. Searches will get slower as the number of items in the flatfile grows.

        hello again, Okay,, One problem hehehe,, How can I sort it lol
        HELP!!! lol