How to sort the data?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilly07
    New Member
    • Jul 2008
    • 89

    How to sort the data?

    Hi I have one row of data from a database as @data.

    I need to sort the items available in $data[1], 2,3,4,5,6,7,8 positions

    For example if the data are as below:

    man, women, man, man, women, man, man, man in their respective position. Then I want my output to be as below:

    134678: man
    25: woman


    How do I do this? I am not so familiar with perl hash as a beginner. It would be of great help if someone helps me out.
    Thanks and Regards
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    You have to create a hash from the array with each unique element as key. Then parse the array and concatenate the position value for each element matching the key, as follows:

    Code:
    @data=(man, women, man, man, women, man, man, man);
    
    foreach(@data) {
     $entry{$_}++; 
     #hash with each unique data as key & number of occurence as its value
    }
    
    foreach my $key (sort keys %entry) {
     my $i=1;
     my $str;
      foreach(@data) {
      $str.="$i" if(/$key/); #concatenate the position 
      $i++;
    }
     print "$str: $key\n";
    }

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      another way using a hash of arrays:

      Code:
      @data = qw(man women man man women man man man);
      my %hash;
      my $i;
      foreach my $thing (@data) {
         push @{$hash{$thing}},$i++;
      }
      for (keys %hash) {
         print "$_:", join(',',@{  $hash{$_} }), "\n"; 
      }

      Comment

      • alampally
        New Member
        • Jul 2008
        • 5

        #4
        Hi Lilly,


        Good Luck,

        Write me for any help in C, C++, Perl, PHP and UNIX related stuff. I will answer you with in the time with free of cost.

        Rammohan Alampally,
        HP Technologies
        Bangalore
        Last edited by eWish; Jul 17 '08, 03:36 AM. Reason: Please do not use BOLD tags for your entire post.

        Comment

        • jain236
          New Member
          • Jul 2007
          • 36

          #5
          Originally posted by alampally
          Hi Lilly,


          Good Luck,

          Write me for any help in C, C++, Perl, PHP and UNIX related stuff. I will answer you with in the time with free of cost.


          Rammohan Alampally,
          HP Technologies
          Bangalore

          Hi ramohna may a simple way, no to complicate life with hasesh
          Code:
          @data=('man', 'women', 'man', 'man',
                 'women', 'man', 'man', 'man');
          my $str;
          my $women;
          for ($i = 1;$i <=$#data+1;$i++)
          {
            $str.= $i if $data[$i-1]=~ /man/;
             $women.= $i if $data[$i-1]=~ /women/;
              
          }
          print "$str:man\n";
          print "$women:women";

          Comment

          • lilly07
            New Member
            • Jul 2008
            • 89

            #6
            Thanks for the help.

            Comment

            Working...