Loading ...
Sorry, an error occurred while loading the content.

Ordering a Hash

Expand Messages
  • nunleva
    Hello everyone, I´m trying to make a sub that returns an ordered hash where the keys are numbers! Ex: %hash= ( 12= Porto , 1= El Boss , 8= Moi , 7= Je ,
    Message 1 of 4 , Jul 1 4:26 AM
    • 0 Attachment
      Hello everyone,

      I´m trying to make a sub that returns an ordered hash where the keys
      are numbers!

      Ex:

      %hash= (
      12=>"Porto",
      1=>"El Boss",
      8=>"Moi",
      7=>"Je",
      4=>"Me"
      );

      What i want to do is a function that returns an Hash with:

      %ordered_hash= (
      1=>"El Boss",
      4=>"Me",
      7=>"Je",
      8=>"Moi",
      12=>"Porto"
      );


      I made this function, but the returned hash is not ordered!??


      sub order_hash{

      my(%hash)=@_;
      my @keys;
      my $value;
      my %hash_ordered;
      my $key;
      @keys=sort {$a<=>$b} (keys %hash);
      foreach $key (@keys) {
      $value=$hash{$key};
      $hash_ordered{$key}=$value;
      }
      return %hash_ordered;
      }

      Can someone help me??

      Thanks!

      Nuno
    • Gordon Stewart
      At 11:26 PM 7/1/03 nunleva wrote:- ... foreach $line (sort keys %hash){ print $line n ; } ... [Non-text portions of this message have been removed]
      Message 2 of 4 , Jul 1 4:33 AM
      • 0 Attachment
        At 11:26 PM 7/1/03 nunleva wrote:-

        >Hello everyone,
        >
        >I´m trying to make a sub that returns an ordered hash where the keys
        >are numbers!
        >
        >Ex:
        > return %hash_ordered;
        >Can someone help me??
        >Thanks!


        foreach $line (sort keys %hash){
        print $line<BR>\n";
        }


        >Nuno
        >
        >
        >Yahoo! Groups Sponsor
        ><http://rd.yahoo.com/M=249982.3512844.4795476.1261774/D=egroupweb/S=1705006951:HM/A=1524963/R=0/SIG=12o72ctft/*http://hits.411web.com/cgi-bin/autoredir?camp=556&lineid=3512844&prop=egroupweb&pos=HM>691fc3.jpg
        >
        >692052.jpg
        >
        >Unsubscribing info is here:
        ><http://help.yahoo.com/help/us/groups/groups-32.html>http://help.yahoo.com/help/us/groups/groups-32.html
        >
        >
        >Your use of Yahoo! Groups is subject to the
        ><http://docs.yahoo.com/info/terms/>Yahoo! Terms of Service.
        >---
        >Incoming mail is certified Virus Free.
        >Checked by AVG anti-virus system (http://www.grisoft.com).
        >Version: 6.0.493 / Virus Database: 292 - Release Date: 6/25/03



        [Non-text portions of this message have been removed]
      • Damien Carbery
        ... You can t order a hash. If you go through each of the keys of a hash, the order the keys are seen is not defined. You can only do as Gordon has
        Message 3 of 4 , Jul 1 5:34 AM
        • 0 Attachment
          --- In perl-beginner@yahoogroups.com, Gordon Stewart <gordon52@s...>
          wrote:
          > At 11:26 PM 7/1/03 nunleva wrote:-
          >
          > >Hello everyone,
          > >
          > >I´m trying to make a sub that returns an ordered hash where the keys
          > >are numbers!
          > >
          > >Ex:
          > > return %hash_ordered;
          > >Can someone help me??
          > >Thanks!
          >
          >
          > foreach $line (sort keys %hash){
          > print $line<BR>\n";
          > }
          >
          >
          > >Nuno
          > >

          You can't order a hash. If you go through each of the keys of a hash,
          the order the keys are seen is not defined. You can only do as Gordon
          has demonstrated, sort the keys as you reference them.

          As your keys are numbers and (I believe) 'sort' by default does string
          comparisons, you can use:

          foreach my $key (sort { $a <=> $b } keys %hash){
          print "$key = $hash{$key}\n";
          }

          to do numeric comparisons.
          I think 'sort keys %hash' is shorthand for:
          'sort { $a cmp $b } keys %hash' because 'cmp' is for string comparisons.

          See:
          http://www.perldoc.com/perl5.8.0/pod/perlop.html#Equality-Operators
        • jarheadfritsch
          I m not sure if this helps but I was faced with the same problem. My program searched through a file for a certain string. Once it found it my program would
          Message 4 of 4 , Jul 9 1:51 AM
          • 0 Attachment
            I'm not sure if this helps but I was faced with the same problem.
            My program searched through a file for a certain string. Once it
            found it my program would push it into a hash. The hash keys were
            all the line number the pattern was matched on. The value was the
            whole line that the pattern was matched in. At the end of it all I
            had a hash with a bunch of randomly stored key value pairs. I then
            set the program to sort the keys, and print them into a file in
            numerical order by line number. Below is the sub and loop I used to
            accomplish this. Hope it helps

            -Robert-

            print "Opening logfile\n";

            open LOG, ">>logfile" or die "Can't open log: $!\n";

            sub by_number { $a <=> $b };

            my @array = keys %hash;
            my @sortedkeys = sort by_number @array;

            foreach (@sortedkeys){
            my $value = $hash{$_};
            print LOG "$_\t$value\n";
            }

            close Log;
            print "Done\n";


            --- In perl-beginner@yahoogroups.com, "nunleva"
            <nuno.fernandes@z...> wrote:
            > Hello everyone,
            >
            > I´m trying to make a sub that returns an ordered hash where the
            keys
            > are numbers!
            >
            > Ex:
            >
            > %hash= (
            > 12=>"Porto",
            > 1=>"El Boss",
            > 8=>"Moi",
            > 7=>"Je",
            > 4=>"Me"
            > );
            >
            > What i want to do is a function that returns an Hash with:
            >
            > %ordered_hash= (
            > 1=>"El Boss",
            > 4=>"Me",
            > 7=>"Je",
            > 8=>"Moi",
            > 12=>"Porto"
            > );
            >
            >
            > I made this function, but the returned hash is not ordered!??
            >
            >
            > sub order_hash{
            >
            > my(%hash)=@_;
            > my @keys;
            > my $value;
            > my %hash_ordered;
            > my $key;
            > @keys=sort {$a<=>$b} (keys %hash);
            > foreach $key (@keys) {
            > $value=$hash{$key};
            > $hash_ordered{$key}=$value;
            > }
            > return %hash_ordered;
            > }
            >
            > Can someone help me??
            >
            > Thanks!
            >
            > Nuno
          Your message has been successfully submitted and would be delivered to recipients shortly.