can't pass a hash from main program due to explicit package

M

monte

hi, I am at it again and so I got a new question.
please assume that the files are correct and not empty. the format of
the files are as follows:

lib0 cell1
lib2 cell2
lib3 cell4
lib5 cell5
etc...

1)I'd like to use a previous hash with the keys=>$cells found in the
main. however, when I try passing it to the subroutine, it complains of
explicit package.
2)turned off strict to check the contents of the hash in the subroutine
but it is empty, how can I bring it and compare to the newly parse
cells to be check?




CODE:


---------------------------------------------------------
#!usr/bin/intel/perl -w
# prog to report duplicate cells found in the heirarchy
# format: level lib cell view
#use strict;

#%main::rec();
my $show_file={};
my $rec={};
my $cell={};
my $lib={};
my @dump=();
my @cell=();
my $cadcell=();




#------------------------------------------------------------------


#file to output cells found in different libraries
my $file = "/tmp/perl/rec.err";
open(ERR ,">$file") || die "cannot open new file: $!\n";


#file contains possible bad cells, but not all.format:<lib> <cell>
my $tfile = "/tmp/perl/fake.ls";
open(BADCELLIST,"<$tfile") || die "cannot open new file: $!\n";


#-----------------------------------------------------------------
#parsing the GOOD file of cells from command line(source of cells)

while(<>)
{

$show_file=$_;
#print $show_file;

if ($show_file=~/^\S+\s+\d\.\s+(\S+)\s+(\S+)/ ||
$show_file=~/^\d\.\s+(\S+)\s+(\S+)/)
{
my ( $cell, $lib)= ($2, $1);
$rec->{$cell}->{$lib}=1;
#making the cell & lib to exist as a hash later to be used as error
finder and for comparing to $cadcells found in my directory
}
}

#&error_check;
#----------------------------------------------------------
#made easier to read by assigning a variable. interested in finding
$cells in different lib for error checking

sub error_check
{
foreach my $cell (keys %$rec)
{
my @contents = sort keys %{ $rec->{ $cell } };
if (@contents == 1)
{
print "$cell (@contents)\n";
}
if( @contents >1 )
{
print ERR "$cell (@contents) -error?";
}
}

}
#----------------------------------------------------------------
# HELP!!!!! here is where the I'd like to use the previous $rec with my
$cell as the key to compare to the new $cadcell in order to find the
ones that do not exits and dump them.
#problem is that I cannot sucessfully pass the previous $rec with
keys=>$cell to compare it to new cadcells!


&comp_2_cell_list;

sub comp_2_cell_list
{
foreach(<BADCELLIST>)
{
if(~/^\s?\S+\s+(\S+)/)
{
my $cadcell=$1;
if(! exists($rec{$cadcell}))
{
# push(@dump,$cadcell,"\n");
# print @dump;
print " the real thing:$rec{$cadcell}\n"; #problem here, it is
empty???
}
}
}
}
 
M

monte

I am not quite sure on the difference and how to use %rec vs $rec but I
will read up on it. however, I will appreciate your input on this to
help me better understand how hashes are passed and the differences.
thank a lot :^)
 
M

monte

why do I get the output with a bad cell name repeating multiple times?
what's wrong with my logic? I like to get a single copy of a bad cell
name, can you point me out to a better algorithm?

sub comp_2_cell_list
{
foreach(<TEST>)
{
if(~/^\s?\S+\s+(\S+)/)
{
my $cadcell=$1;
if(! exists($rec->{$cadcell}))
{
push(@dump,$cadcell,"\n");
print @dump;
}
}
}
}
 
T

Tad McClellan

monte said:
why do I get the output with a bad cell name repeating multiple times?


You have not provided a complete program that we can run (including
the data) so none of us can tell you why you get the output with a
bad cell name repeating multiple times.

what's wrong with my logic?


Not providing a short and complete program *that we can run*.

Have you seen the Posting Guidelines that are posted here frequently?

foreach(<TEST>)


Why do you read the entire file into memory when your algorithm
does not require that the entire file fit into memory?

if(~/^\s?\S+\s+(\S+)/)
^
^

Why have you used the bitwise complement operator there?
 
B

Bob Walton

monte said:
I am not quite sure on the difference and how to use %rec vs $rec but I
will read up on it. however, I will appreciate your input on this to
help me better understand how hashes are passed and the differences.
thank a lot :^)

You need to read up on the Perl documentation. Type:

perldoc perldata

at a command prompt. Also:

perldoc perlref
perldoc perllol
perldoc perldoc
perldoc perltoc

etc etc.

Basically, the characters $, @ and % preceding a variable tell
what kind of value will be delivered or accepted by that variable
($=scalar; @=array; %=hash). So:

$scalar <--a scalar
@array <--an array
%hash <--a hash

The elements of an array are specified using [...], as in:

$array[3] <--the fourth element of array @array --
but note that since the element itself
is a scalar value, a $ is used.

The elements of a hash are specified using {...}, as in:

$hash{key} <--the "key" element of hash %hash --
and note again that this element is
a scalar value, so $ is used.

You can also have "slices":

@array[3,4,5] <--array slice returning elements
4 5 and 6 of array @array -- and
note that since the slice's value
is an array, @ is used.
@hash{'key1','key2'} <--hash slice returning elements
key1 and key2 of hash %hash --
note that since the value is an
array, @ is used.

When you specify $ref->{key}, you are using a hash reference,
usually to an anonymous hash (one without a name). A reference
is a scalar, so the scalar $ref is used. Note that the
namespaces for scalars, arrays and hashes are all separate, so
the referenece $ref has absolutely nothing to do with the hash
%ref, which is what is used in $ref{key}. They are two different
variables, even though they share the same name. So:

$ref->{key}='value1';
$ref{key}='value2';
print "value of \$ref->{key}=$ref->{key}\n";
print "value of \$ref{key} =$ref{key}\n";

will print:

D:\junk>perl junk529.pl
value of $ref->{key}=value1
value of $ref{key} =value2

D:\junk>

HTH.
 
T

Tad McClellan

monte said:
Thank you


Who "you"?

for you help.


What helped?

Please quote some context in followups like everybody else does.

Have you seen the Posting Guidelines that are posted here frequently?
(for the 3rd time...)

I am still learning and will get better at this
soon.


It would serve you well to learn how to compose a
followup *before* your next followup.
 
M

monte

thank you all for your help with my small program. I have learned so
much and corrected my mistakes. :^)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top