Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HASH Loading - what am I misunderstanding?

Status
Not open for further replies.

BobMCT

IS-IT--Management
Sep 11, 2000
756
US
I am trying to load data from a flat file into a hash defined as this:

hash{key}{0} = 1st field
hash{key}{1} = 2nd field
hash{key}{2} = 3rd field

So I can retrieve it last by referencing hash{key}{0,1 or 2}

Here is what my flat file look like:

law7505-01|kas02021|Contracts 1|email1.address\@sub.domain.edu
law7505-02|lsk02002|Contracts 2|email2.address\@sub.domain.edu
.etc.

Here is my current code:

while (<IFH>) {
chomp;
($Class_name, $NetID, $Course_Desc, $IEmail) = split(/\|/, $_);
$Class_Info{$Class_name} = {
'0' => $NetID,
'1' => $Course_Desc,
'2' => $IEmail
};
}

But the resulting hash isn't keyed by the $Class_name, the elements are just extended beyond the last element so the Class_name from the 2nd line becomes element 5 in the hash etc. Just keeps going on and on.

I know that I am missing something and my brain and my time are growing short. Hopefully someone can see something obvious and advise?

Thank you all so much.
 
Hmmm... there might be a problem somewhere else. Your code works fine for me with the sample data you provided.
 
There is nothing obviously wrong with the code that you've provided except that you aren't using strict or warnings.

Code:
use strict;
use warnings;

I'd strongly advise you to always do so in every script that you make.

Other than that, I don't believe we have enough information to diagnose or even really understand the exact nature of your problem. Feel free to provide more info.

- Miller
 
Thanks all. Note: I ALWAYS use strict and warnings but just don't show it in my snippets.

OK - I think I've resolved this by using different index values. I've modified my loading code to this:

$Class_Info{"$Class_name"}{'0'} = $NetID;
$Class_Info{"$Class_name"}{'1'} = $Course_Desc;
$Class_Info{"$Class_name"}{'2'} = $IEmail;

And as long as I reference the various fields as an index number (i.e.: 0,1,2) it is working as expected.

Thanks again.
 
Can I ask a silly question? Why use a hash like an array? If you want to use index numbers, why not make use of a hash of arrays? After the split, you could do something like:
Code:
$Class_Info{$Class_name} = [$NetID, $Course_Desc, $IEmail];
Then when you need to refer to elements, it's fairly similar to what you're doing now:
Code:
$Class_Info{$Class_name}[0] # This would be the NetID
Or, if you would like to use the hash of hashes, why not use more meaningful names? That will probably make it easier to keep track of later -- "what does 0 refer to again?" For example:
Code:
$Class_Info{$Class_name}{net} = $NetID;
$Class_Info{$Class_name}{desc} = $Course_Desc;
$Class_Info{$Class_name}{email} = $IEmail;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top