The problem I am having is the data in $CAPS[0] end up being the same as $CAPS[1]. Somehow I lost the data from the first file. I think what is happening is that I'm placing a reference into $CAPS[0] to %caps and not a copy. So my question is how do I copy %caps to $caps[$i]?

The code I provided is pretty much the core of the script, so providing more of my code won't do you much good, or might confuse you even more, if that's possible. And you're probably right in that it's all wrong. That's why I'm asking for help.

Here's what I'm trying to do. I have to files with data I want to correlate. The files have a list of signals and their capacitance. Both files have the same list of signals but the capacitance are different and I want to find out by how much.

So I'm reading the files and putting the information in a hash with the signal as the key and the cap as the value, but I have to do this for both files. I can create two separate hash tables which is what I have now, but it doesn't scale. What I mean is I might want to compare two files, or five. That's where I need help; I don't know what data structure to use that would be the most efficient.

You could write a seperate subroutine to just parse the data and return the data back to the calling subroutine but that seems like a bit of overkill and added confusion for you at this point in time. -------------------------------------------------

Looking at the code, where is the relation between the file and the cap value? In addition to knowing the cap value of the signal, I need to know what file the cap value came from. Is that clear? Ok, so the lookup table needs to look like this:

I'm not sure if you are mixed up about something, you declare a hash in the function:

Code

my %cap = {};

and then later you have:

Code

$cap{$net}= proc_sum_resistance() ;

Which is OK, but later still you are storing values in a reference to a hash ($cap):

Code

$cap->{$net} = $end*1e-3 ; #convert fF to pF

$cap->{...} and $cap{...} are two seperate data structures entirely. $cap{...} stores data in %cap but $cap->{...} does not. Are you running your code with "strict" enabled? -------------------------------------------------

Then you must have declared $cap somewhere otherwise perl would have exited with an error.

So I finally got it to work. Here's what I did:

1, changed $cap->{...} to $cap{...},

Like you said they are two different data structures, but I don't quite understand the subtleties.

One is a hash and one is a reference to a hash, but not the same hash

2, changed "return $cap" to "return %cap", and

I'm don't know what the first one returns, but I'm sure the second returns a hash.

The first one returns the reference $cap, the second one does return the hash %cap but as a flattened list, that can be very important to understand.

3, changed $CAP_VAL[$num] t0 %{ $CAP_VAL[$num] }.

This was the key. I'm not sure why, but the %{ } around the array made sure I captured the complete array.

Without seeing more context that isn't helpful for me to know why that makes your code work. %{} is dereferencing $CAP_VAL[$num] into its hash form, but that needs to be assigned to something or used to loop over the hash, for example, this would make sense if $CAP_VAL[$num] is a reference to a hash: