Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How to place Search Results!!!

by ACJavascript (Acolyte)
on Aug 04, 2003 at 21:17 UTC ( [id://280807]=perlquestion: print w/replies, xml ) Need Help??

ACJavascript has asked for the wisdom of the Perl Monks concerning the following question:

Hello All I SEEK THE PERL MONKS WISDOM!!

Okay hers my problem.
I have a simple database setup like so

Item1|Item2|Item3

Now I do a simple search on it like so:
@results=grep(/$searchString/i,@da); if($#results >= 0){ foreach $i(@results){ ($item1,$item2,$item3) = split(/\|/,$i);


It then prints out the results if there are any.

Okay now say $item one is a price - 35.00.. Now I want the search results to be printed out from lowest to highest.

The problem here is if im using a database and it goes there each line one by one,, and the first one is 100 dollars and the 5th is 1.00 dollar how can I get it to write out the first one after its past it.

I know this sounds very confusing its been buging me for a while now. Please can someone help!!

Thanks in advance!

Replies are listed 'Best First'.
Re: How to place Search Results!!!
by Mr. Muskrat (Canon) on Aug 04, 2003 at 21:24 UTC
    perldoc -f sort
Re: How to place Search Results!!!
by diotalevi (Canon) on Aug 04, 2003 at 21:23 UTC

    Sort the results by your dollar cost.

      Hey Thanks all for your advice!
      I will be trying it all!
Re: How to place Search Results!!!
by sgifford (Prior) on Aug 05, 2003 at 02:39 UTC

    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";
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://280807]
Approved by tcf22
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-07-16 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.