How do I pass a hash to a function in Perl?

后端 未结 9 908
囚心锁ツ
囚心锁ツ 2020-12-24 05:36

I have a function that takes a variable and an associative array, but I can\'t seem to get them to pass right. I think this has something to do with function declarations, h

相关标签:
9条回答
  • 2020-12-24 06:16

    Arguments to functions get flattened into a single array (@_). So it's usually easiest to pass hashes to function by reference.

    To create a HASH:

    my %myhash = ( key1 => "val1", key2 => "val2" );
    

    To create a reference to that HASH:

    my $href = \%myhash
    

    To access that hash by reference;

    %$href
    

    So in your sub:

    my $myhref = shift;
    
    keys %$myhref;
    
    0 讨论(0)
  • 2020-12-24 06:20

    This code works:

    #!/bin/perl -w
    
    use strict;
    
    sub PrintAA
    {
        my($test, %aa) = @_;
        print $test . "\n";
        foreach (keys %aa)
        {
            print $_ . " : " . $aa{$_} . "\n";
        }
    }
    
    my(%hash) = ( 'aaa' => 1, 'bbb' => 'balls', 'ccc' => \&PrintAA );
    
    PrintAA("test", %hash);
    

    The key point is the use of the array context in the my() 'statement' in the function.


    What does the array context business actually do?

    Succinctly, it makes it work correctly.

    It means that the first value in the @_ array of arguments is assigned to $test, and the remaining items are assigned to the hash %aa. Given the way I called it, there is an odd number of items in the @_, so once the first item is assigned to $test, there is an even number of items available to assign to %aa, with the first item of each pair being the key ('aaa', 'bbb', 'ccc' in my example), and the second being the corresponding value.

    It would be possible to replace %aa with @aa, in which case, the array would have 6 items in it. It would also be possible to replace %aa with $aa, and in that case, the variable $aa would contain the value 'aaa', and the remaining values in @_ would be ignored by the assignment.

    If you omit the parentheses around the variable list, Perl refuses to compile the code. One of the alternative answers showed the notation:

    my $test = shift;
    my(%aa) = @_;
    

    This is pretty much equivalent to what I wrote; the difference is that after the two my statements, @_ only contains 6 elements in this variation, whereas in the single my version, it still contains 7 elements.

    There are definitely other questions in SO about array context.


    Actually, I wasn't asking about the my($test, %aa) = @_; I was asking about my(%hash) = ( 'aaa' => 1, 'bbb' => 'balls', 'ccc' => \&PrintAA ); versus my %hash = { 'aaa' => 1, ... };

    The difference is that the { ... } notation generates a hash ref and the ( ... ) notation generates a list, which maps to a hash (as opposed to hash ref). Similarly, [ ... ] generates an array ref and not an array.

    Indeed, change the 'main' code so it reads: my(%hash) = { ... }; and you get a run-time (but not compile time) error - treat line numbers with caution since I've added alternative codings to my file:

    Reference found where even-sized list expected at xx.pl line 18.
    ...
    Use of uninitialized value in concatenation (.) or string at xx.pl line 13.
    
    0 讨论(0)
  • 2020-12-24 06:23

    All the above methods work, but this was always the way I preferred to do things like this:

    sub PrintAA ($\%)
    {
        my $test       = shift;
        my %aa         = ${shift()};
        print "$test\n";
        foreach (keys %aa)
        {
            print "$_ : $aa{$_}\n";
            $aa{$_} = "$aa{$_}+";
        }
    }
    

    Note: I also changed your code a bit. Perl's double-quoted strings will interpret "$test" to be the value of $test rather than the actual string '$test', so you don't need that many .s.

    Also, I was wrong about how the prototypes work. To pass a hash, use this:

    PrintAA("test", %hash);
    

    To print a hash reference, use this:

    PrintAA("test", %$ref_to_hash);
    

    Of course, now you can't modify the hash referenced by $ref_to_hash because you're sending a copy, but you can modify a raw %hash because you're passing it as a reference.

    0 讨论(0)
提交回复
热议问题