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

I'm a complete newbie at perl. Trying to teach myself this and am having a difficult time solving this particular probelm: I have a flat file db that looks like this: TYPE, URL, NAME, DESCRIPTIONS. My script searches the file and pulls all matching records. Really simple. It displays the output as follows: TYPE NAME It returns the records in a first come first out kind of way. What I'd like to do is sort the records by TYPE and have the ouput display the URL's under the differing types. I guess the question is, how can I make the first field the category by which my database is sorted and its output presented? Is there a sort function I could put into the script to handle this, or would it be better to separate records into different flat files according to their type/category?

Replies are listed 'Best First'.
Re: Newbie help
by chromatic (Archbishop) on May 06, 2000 at 01:59 UTC
    The sort you choose depends on how your data is structured. Have a look at sort and see if that is of any assistance.

    If you use a list of lists, you can do something like: my @sorted = sort { $a[0] cmp $b[0] } @records; If you use a list of hashes, you could do: my @sorted = sort { $a{TYPE} cmp $b{TYPE} } @records; If you're using just an array... you're in for more trouble, and you might want to look at using SQL commands with the DBI module from CPAN.

Re: Newbie help
by btrott (Parson) on May 06, 2000 at 01:59 UTC
    I would investigate perllol, perldsc, and perlref. They'll tell you how to build multi-dimensional data structures in Perl.

    Also look at the sort docs; on the whole, though, your sort function will be pretty simple if you create your data structure wisely.

    If it were me, I'd create a hash where the keys are your TYPEs, and the values are array references; these array references would hold the "records" read in from your file. This code will help to get you started setting up such a structure:

    my %data; while (<>) { chomp; my %rec; @rec{ qw/type url name description/ } = split /,/; push @{$data{$rec{'type'}}}, \%rec; }
    You can take a look at the resultant data structure by using Data::Dumper:
    use Data::Dumper; print Dumper \%data;
    As chromatic said, if you're going to do much more than simple database work, take a look at DBI. DBD::CSV might come in handy for you, or you may want to look at a "real" database, like MySQL. SQL will do a lot of the work for you implicitly if you store your data in a database.
RE: Newbie help, again
by Adam (Vicar) on May 06, 2000 at 01:50 UTC
    It sounds like you are asking us to do a large part of your project for you, which is not really what this site is here for. Yes, perl will allow you to create a sort routine (must return 0,-1, or 1) and to then use that routine to sort an array. There are also good methods for formatting your output to look the way you want it to. But to give you a complete answer I think I would have to type in most of the first half of the Camel book.