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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
guadalupeConnect With a Mentor Commented:
#!/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
 
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
All Courses

From novice to tech pro — start learning today.