简体   繁体   中英

What's wrong with this Perl code to push a hash onto an array?

I'm trying to make an array of hashes. This is my code. The $1, $2, etc are matched from a regular expression and I've checked they exist.

Update: Fixed my initial issue, but now I'm having the problem that my array is not growing beyond a size of 1 when I push items onto it...

Update 2: It is a scope issue, as the @ACLs needs to be declared outside the loop. Thanks everyone!

while (<>) {
    chomp;
    my @ACLs = ();

    #Accept ACLs
    if($_ =~ /access-list\s+\d+\s+(deny|permit)\s+(ip|udp|tcp|icmp)\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\s+eq (\d+))?/i){

        my %rule = (
            action => $1, 
            protocol => $2, 
            srcip => $3, 
            srcmask => $4, 
            destip => $5, 
            destmask => $6, 
        );
        if($8){
            $rule{"port"} = $8;
        }
        push @ACLs, \%rule;
        print "Got an ACL rule.  Current number of rules:" . @ACLs . "\n";

The array of hashes doesn't seem to be getting any bigger.

You are pushing $rule , which does not exist. You meant to push a reference to %rule :

push @ACLs, \%rule;

Always start your programs with use strict; use warnings; use strict; use warnings; . That would have stopped you from trying to push $rule .

Update: In Perl, an array can only contain scalars. The way complex data structures are constructed is by having an array of hash references. Example:

my %hash0 = ( key0 => 1, key1 => 2 );
my %hash1 = ( key0 => 3, key1 => 4 );
my @array_of_hashes = ( \%hash0, \%hash1 );
# or: = ( { key0 => 1, key1 => 2 }, { key0 => 3, key1 => 4 ] );

print $array_of_hashes[0]{key1}; # prints 2
print $array_of_hashes[1]{key0}; # prints 3

Please read the Perl Data Structures Cookbook .

my %rule = [...]

push @ACLs, $rule;

These two lines refer to two separate variables: a hash and a scalar. They are not the same.

It depends on what you're waning to do, but there are two solutions:

push @ACLs, \%rule;

would push a reference into the array.

push @ACLs, %rule;

would push the individual values (as in $key1 , $value1 , $key2 , $value2 ...) into the array.

You're clearing @ACLs each time through the loop. Your my is misplaced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM