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

hash and array 3

Status
Not open for further replies.

simanek

Programmer
Joined
Jan 19, 2001
Messages
137
Location
US
Does anyone know of a method to assign all of the values of a hash into an array without knowing the keys of the hash? Thanks.

Mike
 
foreach $value (values %yourhash) {
push @yourarray,$value;
} Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
Here's a quicker way:
Code:
@array = values %hash;
The values function already returns a list, so there's no need to step thru the list and push the values into the array, just assign all of them to the array at once.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Exactly what I was thinking, tsdragon. And, if you already have an array and want to push the values of the hash onto it, there's nothing stating that you have to push one element at a time, so
Code:
push @array,values %hash;
...works just fine.

brendanc@icehouse.net
 
Good thinking sophisticate. I thought about adding a caveat to my example warning that it would completely replace the array rather than adding to it. You solved that problem.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
And, if you want the hash values sorted into the array:
Code:
@array = sort values %hash;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top