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

Dear monks
My question refers to assigning object into hash
I'm sending a hash reference to a Main function, inside the function I create objects and assign them into the hash
inside the function I can see all the objects inside the hash
Yet in the main part I can see only the last assigned object
BTW the same happen when I'm pushing the objects into array.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~

&parse_verilog($tool,\%params,\%rm_supply_nets,\%cells); sub parse_verilog{ my ($tool,$params,$rm_supply_nets,$cells,$cells_hash ) = @_; my $in_module = 0; my $module; my $new_cell; open(V,$params->{'verilog_in'}) or die "$tool: Error: can't open s +upplied verilog for reading\n"; my $line = ""; while(<V>){ chomp(); next if /^\s*\/\//; s/\/\/[\S*||\s*]+$//; ## chop eol comments $line .= $_; if(/;\s*$/){ ## indicates end of line. $_ = $line; my @line_arr = split(/\s+/,$_); $line = ""; s/\s*;\s*$//; if(/^\s*module/i){ s/,/ /g; $module = $line_arr[1]; $new_cell = new cell ($module); $in_module = 1; $new_cell->read_interface($_); next; } } if($in_module && /endmodule/i){ $cells->{$module} = $new_cell $new_cell = 0; $in_module = 0; $line = ""; } } close(V); };
Thanks in advance

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: assigning objects into hash
by Corion (Patriarch) on Jun 28, 2010 at 07:18 UTC

    Please post your code in <code>...</code> tags so it renders and downloads nicely!

    You haven't shown us your input data, so it is hard to tell what your program is doing and where it goes wrong. You also don't show the output, nor do you show what you expect. My guess is that in the place where you assign your object, you are either assigning it always with the same key, or you are assigning always the same object.

    You never check whether $module has a sensible value. You may want to check that by modifying your regular expression to

    if (/^\s*module\s+(\w+)/) { $module = $1; print "Found module '$module'\n"; }

    Print out $module and Dumper $cells to see what $module is before and after assigning a new cell.

    Data::Dumper is a great tool to inspect your data structures.

Re: assigning objects into hash
by toolic (Bishop) on Jun 28, 2010 at 12:04 UTC