# do something.... $rec->{com} and other fields are always the same...the last one in input file! } } }

THe problem here is that the $rec that I'm getting is always the same (the last one that was stored). See NOTE1 above. Seems like the same piece of memory is being overwritten for each record (I thought my $rec = {} should allocate memory). $component{}{} is a list of records for a component/pri.

It's a bit hard to answer your question without a better understanding of your data and how it's structured. The answer is if it works like you have it then you are not doing anything technically wrong. But there might be better ways to accomplish whatever it is your script is doing. Have you tried just passing the hash in (not using a reference) and getting back out as a hash?

Code

foo(%hash);

sub foo { my %hash = @_; }

Perl does not send a hash to the function, it just sends a list of strings or scalars, this is called "flattening". But you can reconstruct the hash (or array) by packing it back into a hash on the receiving end like I did above. When you have complex data like you have, it is almost always easier to use a reference to the data, which is just one string (the memory address of the data) which you dereference later to get the data back out of the reference.

It's hard to look at some code like you posted and know if it is doing something the right way or not (at least for me it is) because it may be part of a larger program. At a glance you code is overly complicated and verbose, but you may be just in the process of trying to learn how to do things, like use references and subroutines and such and the code is not meant to be as well written as maybe it could be. Not that there is anything terribly wrong that I can see, overly verbose code is better than too terse in my opinion. -------------------------------------------------

Thanks for all the help kevin. This is not part of the larger program. That's all there is. I'm learning to write perl programs. I guess as I write more programs (intelligently) I'll learn. It is now working though :) thanks.