in reply to Sort array according to a value in each element?

I'm not certain how to approach this problem: split or regex? hash or sort()?
I'd add 'databases' to that list of possible solutions although for most circumstances the other answers in this thread are better.
#!perl -w use strict; use DBI; my $aryref = [ ['Item1','2 foo','2 bar'] , ['Item2','0 foo','1 bar'] , ['Item3','1 foo','3 bar'] , ['Item4','1 foo','2 bar'] ]; my $dbh=DBI->connect('dbi:AnyData:'); $dbh->ad_import('tmp','ARRAY',$aryref,{col_names=>'c1,c2,c3'}); printf "%s\n",join ' ',@$_ for @{ $dbh->selectall_arrayref( 'SELECT * FROM tmp ORDER BY c3 DESC, c2 DESC' )}; __END__

Replies are listed 'Best First'.
Re: Re: Sort array according to a value in each element?
by McMahon (Chaplain) on May 24, 2004 at 19:13 UTC
    jZed, my first thought was "this would be easy if it were SQL".

    But the example data is really brain-dead; the real data is interspersed throughout a gigantic text file, and normalizing it for a database isn't really an option.

    Update: jZed's reply is right on. I didn't read closely enough. In fact, I didn't realize DBI was capable of treating an arrayref as a database and getting to it via SQL.

    I'm knee-deep in the Guttman/Rosler paper at the moment (good information there), but this is definitely worth exploring. Soon.
      the real data is interspersed throughout a gigantic text file, and normalizing it for a database isn't really an option.
      I'm sure you're right. But look at my example again. No file or external database is created and no normalizing is done at all. It just takes the same $arrayref you started with and creates a temporary-in-memory table and allows you to query the $arrayfef as if it were a database.