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

Hi there. I'm working with a script that generates a HTML page containing the results of a search. It works basically searching specific field on a pipe delimited file and returns a list of the results found. It's being easy so far.

What I need to do now is to sort this results, so I have two questions:

1) How can I sort this results in alphabetical order?
2) How can I sort this results in numeric order?

The routine that generates the file works on a simple "foreach" statement, like this:
foreach $results (@results){ ($field_1,$field_2,$field_3) = split(/\|/,$results); print<<feach; <tr> <td bgcolor="#C0C0C0" colspan="2"><font face="Verdana, Arial, Helv +etica, sans-serif" size="2">$field_1</font></td> </tr> feach }
The search subroutine goes like this:
sub search_database{ my $search_for = $_[1]; open(DB, $_[0]) or die "Error opening file: $!\n"; while(<DB>){ if($search_field eq "all"){ if(/$search_for/oi){push @results, $_}; } else { ($field_1,$field_2,$field_3)=split(/\|/); if(${$search_field} =~ /$search_for/oi){push @results, $_}; } } close (DB); }
Thanks in advance,

Er Galvão Abbott
a.k.a. Lobo, DaWolf
Webdeveloper

Replies are listed 'Best First'.
RE: Sorting search results in PERL
by AgentM (Curate) on Oct 15, 2000 at 01:48 UTC
    Look up the sort function and callit on your data set. This may not be the best solution for a search engine since you would expect it to be called multiple times on the same data so the sort would always be the same. What you should try to do, if it's possible, is organize your file, preferably with some class of AnyDBM_File. This can also serve to speed up your search engine.
    AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.
      I've got a feeling that I'm almost there, but I'm still missing something... Take a look how my code looks right now:
      @sorted = sort {uc($a) cmp uc($b)} @results; foreach $sorted (@sorted){ ($field_1,$field_2,$field_3) = split(/\|/,$sorted); print<<feach; <tr> <td bgcolor="#C0C0C0" colspan="2"><font face="Verdana, Arial, Helv +etica, sans-serif" size="2"><a href="/relatorios/$user/$razao_social. +data" target="_top">$razao_social</a></font></td> </tr> feach }
      It ALMOST worked, since I've got a result sorted like this (notice that I'm putting here the first letter of each field):

      C
      J
      L
      B
      C
      L
      M
      P
      Q
      R
      S
      T

      Can you tell what I'm doing wrong?
      Tx in advance,

      Er Galvão Abbott
      a.k.a. Lobo, DaWolf
      Webdeveloper
        Well, something is still amiss in the information you're providing. There's no sample dataset to see exactly what's going, so I made some up. The code sorts the data as I would expect, although the print statement makes no use of any of the $field_x variables that you have created.

        I've commented out the print portion, since it produces no output relevant to your question. As you can see, the basic sort and printing for the variables you created works as one would expect.

        I would assert that you are not using 'use strict' and -w in your script, which will cause you endless grief, the scorn of your ancestors, and a generally low karma value. Try adding them, clean the resulting errors up, and see if that helps a little.
        #!/usr/local/bin/perl -w use strict; my @results = (qw(Z|a|b B|c|d H|e|f I|g|h K|i|j M|k|l E|m|n U|o|p P|q| +r Q|s|t A|u|v)); my @sorted = sort {uc($a) cmp uc($b)} @results; foreach my $sorted (@sorted) { my ($field_1, $field_2, $field_3) = split(/\|/,$sorted); print "$field_1, $field_2, $field_3\n"; # my $user = 'arf'; # my $razao_social = 'spot'; # print<<feach; # <tr> # <td bgcolor="#C0C0C0" colspan="2"> # <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> # <a href="/relatorios/$user/$razao_social.data" target="_top +">$razao_social</a> # </font> # </td> # </tr> #feach }

        A, u, v B, c, d E, m, n H, e, f I, g, h K, i, j M, k, l P, q, r Q, s, t U, o, p Z, a, b


        --Chris

        e-mail jcwren

        just to add abit to learned brother jcrewn's comments, look at the following code:

        jptxs:/home/jptxs $ vi sortExample use strict; while (my @sorted = <DATA>) { print sort @sorted; } __END__ camel llama cheeta owl ant butterfly jptxs:/home/jptxs $ perl sortExample ant butterfly camel cheeta llama owl
        Notice I didn't even have to include any arguements to sort alphabetically. Something to chew on as you work through this one : )

        -- I'm a solipsist, and so is everyone else. (think about it)