diarmuid has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I wrote the following piece of code :
open(VHD,"$vhd_file") || die "Can't open $vhd_file $!\n"; while(<VHD>){ # find all the entities in the current file if (/entity\s+(\w+)\s+is/i) { $1{'name'} = $1; $1{'fname'} = $vhd_file; push @entities, \%1; } if (/architecture (\w+) of (\w+) is/i ... /end $1\;/i) { # we are in the architecture of the entity. my $arch_entity = $2; # match instance_name : entity if (/^\s*(\w+)\s*:\s*(\w+)$/) { print STDOUT "$1\t$2\t$arch_entity\n"; # find the entity and add the deps to it foreach my $ent (@entities) { if ($$ent{'name'} eq $arch_entity) { $$ent{'deps'}{$2}++; } } } } } close VHD; } foreach my $ent (@entities) { print "$$ent{'fname'}\t$$ent{'name'}\n"; foreach my $dep (keys %{ $$ent{'deps'} } ) { print "DEP: $dep $$ent{'deps'}{$dep} "; print "\n"; } }
However when I run the script only the last entity is printed from the last foreach loop. The problem seems to be that the %1 hash is overwritten by each successive match or something like that. I'm not sure. I thought what I did was ok output :
/proj/dice/external/adelante/24July2002/PowerBteFPGA/PBTE_FPGA/ref/coo +lrisc/status_816.vhd status_816 DEP: latchs_reg 34 DEP: pc_stack_816 11 DEP: latchr 367 DEP: latchr_reg 426 DEP: cr816lu 10 /proj/dice/external/adelante/24July2002/PowerBteFPGA/PBTE_FPGA/ref/coo +lrisc/status_816.vhd status_816 DEP: latchs_reg 34 DEP: pc_stack_816 11 DEP: latchr 367 DEP: latchr_reg 426 DEP: cr816lu 10 etc....

Replies are listed 'Best First'.
Re: refs and hashes
by Abigail-II (Bishop) on Jul 29, 2002 at 13:54 UTC
    All you push on @entities is a reference to the hash %1. Repeatedly. References to the same hash over and over again. And you are overwriting the values of said hash.

    It would work if, instead of using %1, you'd use a lexical hash, but it should be declared inside the same block you are pushing onto @entities.

    Abigail

Re: refs and hashes
by stajich (Chaplain) on Jul 29, 2002 at 13:58 UTC
    You could do the following in your first conditional
    if (/entity\s+(\w+)\s+is/i) { push @entities, {'name' => $1, 'fname' => $vhd_file}; }
    I don't think you really want to do %1 setting.
      yea . I kinda knew that when I wrote it . doh ! Sorted it now anyway with a hohoh
      if (/entity\s+(\w+)\s+is/i) { $entities{$1}{'name'} = $1; $entities{$1}{'fname'} = $vhd_file; }
      ta
Re: refs and hashes
by Ay_Bee (Monk) on Jul 29, 2002 at 14:10 UTC
    This piece of code seems suspect to me
    # find all the entities in the current file if (/entity\s+(\w+)\s+is/i) { $1{'name'} = $1; $1{'fname'} = $vhd_file; push @entities, \%1; }
    I think it should be something like
    push @entities, \%{$1};
    perhaps using the Data::Dumper module would help to debug it, also I would use a hash of hashes rather than a separate hash for each entity.
    use Data::Dumper; .... # find all the entities in the current file if (/entity\s+(\w+)\s+is/i) { $hash{$1}{'name'} = $1; $hash{$1}{'fname'} = $vhd_file; push @entities, $1; } .... close VHD; } print Dumper(%hash);
    Hope this is of some use Ay_Bee
    -_-_-_-_-_-_-_-_-_-_-_- My memory concerns me - but I forget why !!!
      push @entities, \%{$1};

      This is not desirable, because this uses a soft reference, which is a (generally) "Bad Thing"(tm), for a host of reasons.

      Best is to use a lexically-scoped hash or a HoHoH's (like the poster has ended up doing).

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.