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

Hello, Could anyone tell me how to take the following code and set it so that when the data is viewed it is in alphabetical order by the "company_name"...which is line_pair[1]? Any help would be greatly appreciated!!
open(DATAFILEIN,"$catagory.dat") || print "This section is + currently empty...Please check back often!"; print "<table border=0 cellpadding=0 border=0>"; while (<DATAFILEIN>) { chomp $_; @line_pair = split(/\|/,$_); $time = $line_pair[0]; $company_name = $line_pair[1]; $email= $line_pair[2]; $member1= $line_pair[3]; $member1phone= $line_pair[4]; $data= $line_pair[5]; $time2 = $line_pair[6]; $data = &stripBadHtml($data); $password= $line_pair[8]; $pictureurl= $line_pair[7]; $website= $line_pair[9]; $member2= $line_pair[10]; $member2phone= $line_pair[11]; $address= $line_pair[12]; $citystatezip= $line_pair[13]; $fax= $line_pair[14];

Fixed '[' in text - dvergin 2002-10-08

Replies are listed 'Best First'.
Re: Alphabetical Order
by jdporter (Paladin) on Nov 09, 2002 at 02:24 UTC
    It looks like some of your code got cut off... but probably there was just a print statement down there. Anyway, I'll offer some tips.

    First thing I'd do is take all those fields and stick them in a hash rather than a bunch of independent variables.

    while (<DATAFILEIN>) { chomp; my %record; @record{ @fields } = split /\|/;
    Then, rather than print it out right there -- since that would mean printing records in the order they come in -- I'll add each record's hash to an array of hashes, for sorting and printing later:
    push @records, \%record; }
    Note that I'll have to define the @fields array before the loop, something like this:
    my @fields = qw( time company_name email member1 member1phone data time2 pictureurl password website member2 member2phone address citystatezip fax );
    Now, after I've gotten all the record hashes into @records, I can sort them by company name and print them out:
    for ( sort { $a->{'company_name'} cmp $b->{'company_name'} } @record +s ) { print "$_->{'company_name'} and other fields here.\n" }

    Another thing you might consider... If the company names are unique, store the records in a hash keyed by company_name, rather than in an array. Something like this:

    while (<DATAFILEIN>) { chomp; my %record; @record{ @fields } = split /\|/; $records{ $record{'company_name'} } = \%record; } # this makes the sorting a bit more straightforward: for ( sort keys %records ) { my %rec = %{ $records{$_} }; print "$rec{'company_name'} and other fields here.\n" }
    Does that make sense? If you have any questions, let me know.

    jdporter
    ...porque es dificil estar guapo y blanco.

Re: Alphabetical Order
by TomDLux (Vicar) on Nov 09, 2002 at 22:15 UTC
    The simplest way to do this is to make sure the file, category.dat, is sorted according to company name.

    Is the file used by other processes? How often is the file modified? How complicated would it be to have the script which modifies the list insert new entries by sorting order?

    TomDLux