Ho to di it in perl ?

in A.reg, I have few lines. And each line has this format  :

   id:date:lastname:firstname:email:topic




date have format of 2/6/2000
there will be two links :    Sort by Date        Sort by Topic

Can experts show me how to make this happen in PERL ?
So, When I clicked on 'Sort by Date', then the lines in A.reg will be sortd by date, same as 'Sort by Topic."

After sorting, it should be able to maintain the as format of

  id:date:lastname:firstname:email:topic

Thank a lot !
john_pet65Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guadalupeCommented:
#!/usr/local/bin/perl

$sort = "date";

open(DB, "sort.db");

@lines = <DB>;

foreach $line (@lines)
{
      @elements = split(/:/, $line);

      
      if ($sort eq "topic")
      {
            $list{ "$elements[5]" } = $line;      
      }

      else
      {
            $list{ "$elements[1]" } = $line;
      }
      

}

foreach $key (sort keys %list)
{
      print $list{$key};
}


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
guadalupeCommented:
Just for your info, this was the sort.db which I used in my tests:

1:2/6/2001:lastname:firstname:email:b
2:1/7/2000:lastname:firstname:email:a
0
john_pet65Author Commented:

Format ->  id:date:lastname:firstname:email:topic

I already have the following in A.reg, like :

5:2/1/2000:John:Mill:john@123,com:topic1
1:2/5/2000:Ng:Wulong:ng08@hotmail.com:topic2
45:2/7/2000:Micheal:wu:wu@123.com:topic3

the order of above 3 lines will be always stay that
way.

So, when user click on "Sort by Date", the function
will open the file and read all lines into an array. Then sort the array by date.


So, when user click on "Sort by Topic", the function
will open the file and read all lines into an array. Then sort the array by topic.

After reading, the lines in A.reg will stay the same,
like :

5:2/1/2000:John:Mill:john@123,com:topic1
1:2/5/2000:Ng:Wulong:ng08@hotmail.com:topic2
45:2/7/2000:Micheal:wu:wu@123.com:topic3

So can I use your perl script to achieve what I want ?
0
guadalupeCommented:
YES!  The only this to be careful of is that I hard coded the type of soting at the begining of the script.  You will need to get the sort type from a form.  
0
guadalupeCommented:
So how did it work?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.