Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > can't pass a hash from main program due to explicit package

Reply
Thread Tools

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

 
 
monte
Guest
Posts: n/a
 
      05-10-2005
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???
}
}
}
}

 
Reply With Quote
 
 
 
 
monte
Guest
Posts: n/a
 
      05-10-2005
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 :^)

 
Reply With Quote
 
 
 
 
monte
Guest
Posts: n/a
 
      05-10-2005
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;
}
}
}
}

 
Reply With Quote
 
monte
Guest
Posts: n/a
 
      05-10-2005
Thank you for you help. I am still learning and will get better at this
soon.

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      05-11-2005
monte <(E-Mail Removed)> wrote:

> 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?

while ( <TEST> )


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

^
^

Why have you used the bitwise complement operator there?


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Bob Walton
Guest
Posts: n/a
 
      05-11-2005
monte wrote:

> 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.
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      05-11-2005
monte <(E-Mail Removed)> wrote:

> 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.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
monte
Guest
Posts: n/a
 
      05-23-2005
thank you all for your help with my small program. I have learned so
much and corrected my mistakes. :^)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM



Advertisments