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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbee Questions - Hash of Hashes / data structures

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
24 hours into my 1st Perl coding, and I'm jumping right into data structures. I'm testing conceptual stuff 1st.I have some questions:

1.) is the line "my %byaid" defined correctly?
2.) where I use "undef"'s, is this right?
3.) why am I not getting what I expect in the output?

Please go easy on me ... thanks!


#! /usr/perl5/5.6.1/bin/perl -w
use strict;
#use Class::Struct;

my %byaid;

my $rec = {
AID => undef,
STATUS => undef,
};

$rec->{AID} = "A";
$rec->{STATUS} = "stat-A";
$byaid{ $rec->{AID} } = $rec;

$rec->{AID} = "B";
$rec->{STATUS} = "stat-B";
$byaid{ $rec->{AID} } = $rec;

$rec->{AID} = "C";
$rec->{STATUS} = "stat-C";
$byaid{ $rec->{AID} } = $rec;

foreach my $x (keys %byaid) {
print "byaid key= ".$x."\n";
printf "AID=%s STATUS=%s\n",
$byaid{$x}{AID},
$byaid{$x}{STATUS};
}


OUTPUT:
byaid key= A
AID=C STATUS=stat-C
byaid key= B
AID=C STATUS=stat-C
byaid key= C
AID=C STATUS=stat-C

I was expecting:
byaid key= A
AID=A STATUS=stat-A
byaid key= B
AID=B STATUS=stat-B
byaid key= C
AID=C STATUS=stat-C
 
ahhhh,

I need to add $rec={}; before assigning additional values.
Like malloc in C ....

$rec={};
$rec->{AID} = "B";
$rec->{STATUS} = "stat-B";
$byaid{ $rec->{AID} } = $rec;

$rec={};
$rec->{AID} = "C";
$rec->{STATUS} = "stat-C";
$byaid{ $rec->{AID} } = $rec;
 
Not sure what you're goal is, but try
Code:
%{$byaid{ $rec->{AID} }} = %{$rec};


If this is you're first prog, you may want to slow down or you're going to hurt yourself;)
 
1.) is the line "my %byaid" defined correctly?
2.) where I use "undef"'s, is this right?
3.) why am I not getting what I expect in the output?

1. Yes. But all it does is declare the variable for later use.

2. Using undef is almost never necessary as the valu eof a variable. You can also define the value as an empty string '' or a 0 (zero) if you want it to have an initial false value.

3. There is no way to answer that because nobody knows what you expect the output to be.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm creating a hash of hashes, where AID is the key of the outer hash (outer has is "byaid"). I was simply consectively pumping data to the "rec" hash structure, and then assigning that structure to the outer byaid hash. I was expecting (as orig. stated) the following:

byaid key= A
AID=A STATUS=stat-A
byaid key= B
AID=B STATUS=stat-B
byaid key= C
AID=C STATUS=stat-C

Instead I was getting the same values for all three records.
Declaring a new memory space for each record does the trick - I'll also try Pinkey's idea:
%{$byaid{ $rec->{AID} }} = %{$rec};

Thanks!

...and no, this is not my 1st program, just my 1st exposure to perl ...
 
Not sure what you're goal is, but try
Code:
%{$byaid{ $rec->{AID} }} = %{$rec};


If this is you're first prog, you may want to slow down or you're going to hurt yourself;)
 
??? Just looking back on some posts - my browser, Firefox, just indicated it had to resend something - next thing I know my post from last Friday is in here again ... I'd say oops or sorry if I knew what happened.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top