What decides the order of keys when I print a Perl hash?

前端 未结 8 1228
挽巷
挽巷 2020-12-03 19:35

activePerl 5.8 based

#!C:\\Perl\\bin\\perl.exe
use strict;
use warnings;

# declare a new hash
my %some_hash;

%some_hash = (\"foo\", 35, \"bar\", 12.4, 2.5,         


        
8条回答
  •  佛祖请我去吃肉
    2020-12-03 19:58

    I went over your code and made some notes that I think you will find helpful.

    use strict;
    use warnings;
    
    # declare a new hash and initialize it at the same time
    my %some_hash = (
        foo   => 35,       # use the fat-comma or '=>' operator, it quotes the left side
        bar   => 12.4,     
        2.5   => "hello",
        wilma => 1.72e30,
        betty => "bye",    # perl ignores trailing commas, 
                           # the final comma makes adding items to the end of the list less bug prone.
    );
    
    my @any_array = %some_hash; # Hash is expanded into a list of key/value pairs.
    
    print "$_ => $some_hash{$_}\n" 
        for keys %some_hash;
    
    print "\n\n",              # You can print multiple newlines in one string.
          "@any_array\n\n";    # print takes a list of things to print.
    
    # In print @foo; @foo is expanded into a list of items to print.  
    # There is no separator between the members of @foo in the output.
    
    # However print "@foo"; interpolates @foo into a string. 
    # It inserts spaces between the members of the arrays.
    
    
    # This is the block form of 'for'
    for my $k (sort keys %some_hash)
    {
        # Interpolating the variables into a string makes it easier to read the output.
        print "$k => $some_hash{$k}\n";
    }
    

    Hashes provide unordered, access to data by a string key.

    Arrays provide access to ordered data. Random access is available by using a numerical index.

    If you need to preserve the order of a group of values, use an array. If you need to look up members of the group by an associated name, use a hash.

    If you need to do both, you can use both structures together:

    # Keep an array of sorted hash keys.
    my @sorted_items = qw( first second third fourth );
    
    # Store the actual data in the hash.
    my %item;
    @item{ @sorted_items } = 1..4;  # This is called a hash slice.  
                                    # It allows you to access a list of hash elements.
                                    # This can be a very powerful way to work with hashes.
    
    # random access
    print "third => $item{third}\n";
    
    
    # When you need to access the data in order, iterate over
    # the array of sorted hash keys.  Use the keys to access the
    # data in the hash.
    
    # ordered access
    for my $name ( @sorted_items ) {
        print "$name => $item{$name}\n";
    }
    

    Looking at your code samples, I see a couple of things you might want to work on.

    • how looping structures like for and while can be used to reduce repeated code.
    • how to use variable interpolation

    BTW, I am glad to see you working on basics and improving your code quality. This investment of time will pay off. Keep up the good work.

提交回复
热议问题