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

Well I clearly I still dont understand how references and hashes fully.I am writing a module with a subroutine in it.

The subroutine builds an array of a hashes (easy), however I have tried to assign an array to one of those hashes and I'm having no luck. Any ideas? I'm pretty sure it something to do with the way I'm assigning to the hash or reading from the hash Here's the code.

sub Parse_Constraint_File { croak("Parse_Constraint_File <filename>") unless(my $filename = shif +t); my $setup_hash_ref = shift; my $open_command = ($filename =~ /\.gz$/) ? "gzip -dc $filename|" : +$filename; my $rec={}; my @path_details=[]; my $i=0; open(CONST,"$open_command") || croak("Can't open $filename"); while(<CONST>){ # find out the important info: if(/Startpoint: ([\w\/]+)$/) { $rec->{'startpoint'} = $1 }; if(/Endpoint: ([\w\/]+)$/) { $rec->{'endpoint'} = $1 }; if(/Path Group: (\w+)$/) { $rec->{'path_group'}= $1 }; if(/Path Type: (\w+)$/) { $rec->{'path_type'}= $1 }; if(/^\s+Point/.../^\s+slack/ && $setup_hash_ref->{'detail'}){ push @path_details,$_; } if(/^\s+slack [\w\(\)]+\s+(-?[\d\.]+)$/) { #last field is reached $rec->{'slack'} = $1; $rec->{'path_details'} = [ @path_details ]; # if we set a startpoint the only pick those paths if (defined($setup_hash_ref->{'startpoint'}) && defined($setup_hash_ref->{'endpoint'})){ if($rec->{'startpoint'} =~ /$setup_hash_ref->{'startpoint'}/ & +& $rec->{'endpoint'} =~ /$setup_hash_ref->{'endpoint'}/){ $timing_paths[$i++] = $rec; print @path_details; } }else{ # otherwise take all points $timing_paths[$i++] = $rec; } @path_details=[]; $rec={}; } } return @timing_paths; close CONST;
PS If anyone spots room for improvement in my code please point it out. It's the only way I'll learn :-) Diarmuid

Replies are listed 'Best First'.
Re: references hashes arrays fun ... again :-(
by premchai21 (Curate) on Aug 09, 2001 at 19:02 UTC
    Well, for one thing, you don't need all those single quotes in the hash accesses, because hash accesses allow for unquoted barewords -- they get quoted automatically, after a fashion -- unless there are characters in it that look like an expression, e.g. + ! @ $ / ... Also, you might want to localize CONST (local *CONST;), because otherwise you're using a global filehandle from a subroutine, which is generally considered bad practice. The return is happening before the close, so the close never occurs. Where is @timing_paths declared (are you using strict?)? And why do you need [$i++] when there's push around?

    Just my \$0.02...

    Update: Why are you assigning [] to an array? [] is an array reference. That means your array will have one item, a reference to an array with no elements. I'm not sure this is what you want...

      I will localize CONST
      @timing_paths is defined outside the subroutine. I am using strict.

        @timing_paths is defined outside the subroutine.
        I hope you have a very good reason for that usage of a global variable ... I can't see any from your snippet.

        -- Hofmator

Re: references hashes arrays fun ... again :-(
by rchiav (Deacon) on Aug 09, 2001 at 19:08 UTC
    $rec->{'path_details'} = [ @path_details ];
    Is this the line you're referring to? If so, I think what you want is..
    $rec->{'path_details'} = \@path_details ;
    What you have, I *think* is creating an array ref of @path_details evaluated in scalar context, which will be the count of the array. I could be completely wrong, because I usually have to fumble through context related issues to figure out what's going on. But on first glance, this is what I see.

    Hope this helps..
    Rich

      The difference between

      $rec->{'path_details'} = [ @path_details ];

      and

      $rec->{'path_details'} = \@path_details;

      is that the first one creates a copy of @path_details and returns the reference to this new copy, whilst the second one returns a reference to the original @path_details.

      This has nothing to do with context.

      --
      <http://www.dave.org.uk>

      Perl Training in the UK <http://www.iterative-software.com>

Re: references hashes arrays fun ... again :-(
by dragonchild (Archbishop) on Aug 09, 2001 at 19:16 UTC
    The line my @path_details = []; seems to be your problem. Whenever you have square brackets like that, you talking about an arrayref. Arrayrefs are scalars. So, that line should be my $path_details = []; instead.

    Now, push won't take an arrayref - it needs an actual array. So, your lines with push now become push @$path_details, ... and that should work.

    As a sidenote, a comment like "Find out the important stuff" is completely and utterly useless. What important stuff? Why is it important? Is this all the important stuff?

    In addition, that if(){}; section in the beginning should be if-elsif-elsif-elsif instead. If it matches the first, it won't match any of the others. Or, you could just add a next to each of the blocks, cause once you have that info, it looks like you just want to read the next line.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

Re: references hashes arrays fun ... again :-(
by snax (Hermit) on Aug 09, 2001 at 19:50 UTC
    OK, so others have pointed out the
    @path_detail = [];
    issue, so I won't mention it again :)

    I'm not terribly enamoured with your use of @path_details in any event. Your ultimate goal is to place this into the %$rec hash (as a reference) and then save the hash refs in @timing_paths. That's OK, but it would be clearer to me if you said something like

    $rec->{path_details} = [];
    after creating/resetting the $rec hash ref, and then using that straightaway as your array ref when using push:
    push @{ $rec->{path_details} }, $_;
    Now you are being explicit about when/where this info is going to be stored, rather than creating this "artificial" @path_details and then plugging it into $rec->{'path_details'} later.

Re: references hashes arrays fun ... again :-(
by diarmuid (Beadle) on Aug 09, 2001 at 19:03 UTC
    Just to clarify, the array in question is @path_details