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

Complex Structures - Getting keys of the Hash

Status
Not open for further replies.

dmazzini

Programmer
Jan 20, 2004
480
US
Hey guys!

I am trying to use complex structures in Perl using hashes.

Let me start with the code:

Imagine a table:

SITECODE BSC BCF BTS TRX
N303 LSANBSC01 1 125 3


%SITE =();
$BSC="LSANBSC01";
$BCFID=1;
$BTSID=125;
$TRX=3;
$SITECODE="N303";

$SITE{$BSC{$BCFID}{$BTSID}{TRX}}=$SITECODE;

print "My Site is $SITE{$BSC{$BCFID}{$BTSID}{TRX}}\n"; # Print the site code

@SITES=values(%SITE);
print "SITES:mad:SITES\n"; #Print the site code (N303)

I would like to know how can I get the the keys of the hash %SITE. and print it.

Many Thanks for the help.

Cheers


 
Shouldn't
Code:
$SITE{$BSC{$BCFID}{$BTSID}{TRX}}=$SITECODE;
be
Code:
$SITE{$BSC}{$BCFID}{$BTSID}{TRX}=$SITECODE;
 
Ok thanks.

I have tested both and they work good!
I am able to print the site code. ($SITECODE) using

$SITE{$BSC{$BCFID}{$BTSID}{TRX}}=$SITECODE; or

$SITE{$BSC}{$BCFID}{$BTSID}{TRX}=$SITECODE;


My question is how can I know the keys of the hash %SITE. ($BSC,$BCFID,$BTSID,$TRX) for each $SITECODE.

For instance If I have the expresion:

$SITE{$BSC}{$BCFID}{$BTSID}{TRX}=$SITECODE;

How Can I get the keys of the hash %SITE.??


I want to do something like:


%hash = ( Karl => 12,
Joe => 43);

# obtain the list of hashKeys and display each key-value pair

@hashKeys = keys( %hash );

for ( $i = 0; $i < @hashKeys; ++$i ) {
print "$hashKeys[ $i ] => $hash{ $hashKeys[ $i ] }\n";
}


Thank you for the help.









 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top