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

Discussion in 'Perl Misc' started by monte, May 10, 2005.

  1. monte

    monte Guest

    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???
    }
    }
    }
    }
    monte, May 10, 2005
    #1
    1. Advertising

  2. monte

    monte Guest

    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 :^)
    monte, May 10, 2005
    #2
    1. Advertising

  3. monte

    monte Guest

    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;
    }
    }
    }
    }
    monte, May 10, 2005
    #3
  4. monte

    monte Guest

    Thank you for you help. I am still learning and will get better at this
    soon.
    monte, May 10, 2005
    #4
  5. monte <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 11, 2005
    #5
  6. monte

    Bob Walton Guest

    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
    Bob Walton, May 11, 2005
    #6
  7. monte <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 11, 2005
    #7
  8. monte

    monte Guest

    thank you all for your help with my small program. I have learned so
    much and corrected my mistakes. :^)
    monte, May 23, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.T. Conklin
    Replies:
    1
    Views:
    440
    David Hilsee
    Aug 11, 2004
  2. Andy
    Replies:
    5
    Views:
    505
    Shezan Baig
    Jan 30, 2005
  3. Fresh
    Replies:
    2
    Views:
    636
    Bo Persson
    Apr 22, 2008
  4. ken_Ident

    Serilization fails due to object in explicit operator

    ken_Ident, Feb 25, 2005, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    151
    Ken_Ident
    Mar 9, 2005
  5. rp
    Replies:
    1
    Views:
    520
    red floyd
    Nov 10, 2011
Loading...

Share This Page