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!

Sorting Hash Arrays - newbie 2

Status
Not open for further replies.

snookmz

Programmer
Apr 17, 2001
46
AU
G'day all

I was wondering if there was a function out there to sort a hash array on the Key.. I know that you can sort on the value, but how would i go about sorting on the key??

Thanks in advance /-------------------------------------
| I always have been, and always will |
| be, a newbie :) |
\-------------------------------------
 
Well.. the very nature of hashes makes them unsortable. But if you're just looking to loop through a hash by sorted key, you can sort the array from the keys() function like so:
Code:
foreach(sort keys %hash) {
    print $hash{$_};
}
If you actually want to sort the hash indefinitely, you could use the DB_File module which, I think, actually resorts each time you access the hash, but it does it all in the background so you don't have to worry about it. :)

Here's another thought, though I don't know if it's really that useful. You could generate a multidimensional array with a reference to the hash as element 1 and the key in element 0. e.g.
Code:
@array = map { [$_,\$hash{$_}] } sort keys %hash;
foreach(@array) {
    print "${$_->[0]}\t$_->[1]\n";
}
Hope one of these helps,

brendanc@icehouse.net
 
Thanks for your help, i ended up using the sort function like you suggested when i print it out..

foreach $key (sort keys(%pageHash)){
print &quot;<tr><td>$key</td><td>$pageHash{$key}</td></tr>\n&quot;;
}

much obliged :0) /-------------------------------------
| I always have been, and
| always will be, a newbie :)
\-------------------------------------
 
I am not a perl programmer but occasionally I stumble across problems that I need to solve in Perl. Now I would need to loop properly through a multidimensional array, and was trying to use &quot;foreach&quot; for that;

example;

$items[0]{title} = &quot;Smoke on the water&quot;;
$items[0]{artist} = &quot;Deep Purple&quot;;
$items[1]{title} = &quot;Kill'em all&quot;;
$items[1]{artist} = &quot;Metallica&quot;;

$x = &quot;0&quot;;

foreach $item ($items) {
print $items[x++]{title}; # this works
print $item{title}; #but this doesn't... why?
}
 
Try this slightly modified version of your code:

Code:
$items[0]{title} = &quot;Smoke on the water&quot;;
$items[0]{artist} = &quot;Deep Purple&quot;;
$items[1]{title} = &quot;Kill'em all&quot;;
$items[1]{artist} = &quot;Metallica&quot;;

foreach $item (@items) {
	print ${$item}{title}, &quot;\n&quot;;
}
The foreach statement needs to use the @items list, not $items. Inside the foreach loop $item will be a hash reference.
 
I'm reading this thinking - hey that's pretty similar to what I'm trying to do, except on a less complicated level.

I have a pair of arrays with file names in them, and then i have a hash of the file names (abc.pdf) and their titles (&quot;thrash metal top 100 ever list&quot;).

What I want to do is loop through my arrays providing links to these documents (from the array data), but I want to show the document titles as the text for the link, from the hash.

I have populated my hash like this;-

for($i=2;$i<@indat;$i++) {
($key, $value) = (split '&', $indat[$i]);
$hashish{$key} = [$value];
}

because the array @indat is just been read in from a file, and the file info starts from the third line in the file.

then i have tried to print out my links like this;-

foreach $cuz(@cfiles) {
print &quot;<tr><td><a href='$relcurfile$cuz' target='_blank'>$hashish{$cuz}</a></td></tr>&quot;;
}

where @cfiles is one of my 2 file name arrays.

I am a bit of a newbie with hashes, not got my head round them yet, so any help would be greatly appreciated.


Many thanks

Jez

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top