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

Howto: Get keys in a hash object? 1

Status
Not open for further replies.

thenewa2x

Programmer
Dec 10, 2002
349
US
i don't know exactly what they are called but i just want the keys/names for the hash in:
[tt]$obj->{'HASH'}[/tt]

i tried this:
Code:
my %hash = $obj->{'HASH'};

foreach my $key (keys %hash)
{
  print $key.' => '.$hash{$key};
}

but that doesn't seem to work.

---------------------------------------
If you helped... thx.
 
When you create the object, are you using bless? Or are you just returning a ref to a hash?

------------------------------

The beatings will stop when morale improves!!!!!
 
blessing it

---------------------------------------
If you helped... thx.
 
See if this helps.....

my $newVar = TEST->testObj();

foreach my $key (keys % { $newVar } ) {

print "$key\n";

}


package TEST;

sub testObj {

#Setup Basic Class
my $class = shift;
my $self = {};

#Create info in hash
$self->{ONE} = "1";
$self->{TWO} = "2";
$self->{THREE} = "3";

#Return the object
bless($self, $class);
return($self);

}#END sub

------------------------------

The beatings will stop when morale improves!!!!!
 
thx, very nicely done

---------------------------------------
If you helped... thx.
 
You are very welcome! Glad I could help.

------------------------------

The beatings will stop when morale improves!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top