CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
sorting a hash of an array

 



slauzier
Deleted

Feb 1, 2001, 2:20 PM

Post #1 of 6 (655 views)
sorting a hash of an array Can't Post

im using the keys as the headers for my data, and the indeces of the array for my rows. and i want to sort asciibetically by a specified column, but the keep the data for each row together. there are about 500 rows and 9 columns. i realize this isnt probably the best data structure, but i have a lot of stuff already working with this, and just need this sort feature. i just cant seem to get it working right. any help would be greatly appreciated.



Jasmine
Administrator / Moderator

Feb 1, 2001, 9:11 PM

Post #2 of 6 (648 views)
Re: sorting a hash of an array [In reply to] Can't Post

The code to use depends on how your hash is set up -- are you using an anonymous hash or just tossing the array into brackets like a multidimensional hash?

Here's a multidimensional example:


Code
#!/usr/bin/perl 

use strict;

my @array1 = (1 .. 50);
my @array2 = (50 .. 100);
my @array3 = (150 .. 200);

my %hash = (
1 => [@array1],
2 => [@array2],
3 => [@array3],
);

my $sortbykey = 5;

for my $key (sort {$hash{$a}[$sortbykey] <=> $hash{$b}[$sortbykey]} keys %hash){
print "$key - Sorted by $sortbykey, whose value is $hash{$key}[$sortbykey]\n";
}

Here's an anonymous hash example:


Code
#!/usr/bin/perl 

use strict;

my @array1 = (1 .. 50);
my @array2 = (50 .. 100);
my @array3 = (150 .. 200);

my $hash = {
1 => [@array1],
2 => [@array2],
3 => [@array3],
};

my $sortbykey = 5;

for my $key (sort {$hash->{$a}[$sortbykey] <=> $hash->{$b}[$sortbykey]} keys %$hash){
print "$key - Sorted by $sortbykey, whose value is $hash->{$key}[$sortbykey]\n";
}

If neither of these work, please post the code that shows how the hash is propagated.



slauzier
Deleted

Feb 2, 2001, 10:59 AM

Post #3 of 6 (639 views)
Re: sorting a hash of an array [In reply to] Can't Post

well, that is kinda what i have now.....but i dont want to sort all the keys like that, i just want to sort one of the columns, but print out the all the data sorted by that column.(im not sure if im making any sense here, so tell me if im not:) here is how i create the hash(its created on the fly so more columns or more rows could possibly be added very easily):

Code
#key_value is an array declared at the beginning, with 9 values 

sub read_in_hash {
#@list is a line by line dump from the file
$i = 0;
@list = split /,/;
foreach $item (@list) {
if ($item !~ /\w/) {
$item = "<BR>";
}
$data{$key_value[$i]}[$num_rows] = $item;
$i++;
}
$num_rows++;
}

so then i a column(key) would be chosen to sort by. I would sort the column, then print out the results in table format like this....

Code
for ($i = 0; $i < $num_fields; $i++) { 
print "<TH>$key_value[$i]";
}

for ($j = 0; $j < $num_rows; $j++) {
print "<TR>";
for ($i = 0; $i <= $num_fields; $i++) {
print "<TD>$data{$key_value[$i]}[$j]";
}
}

so what do you think....i know there is way (there is always a way with perl, or so im told:)




Jasmine
Administrator / Moderator

Feb 2, 2001, 3:03 PM

Post #4 of 6 (637 views)
Re: sorting a hash of an array [In reply to] Can't Post

Perhaps I'm not completely understanding you -- the code that I posted sorts based on a column you define, not the keys of the hash.


Code
my $sortbykey = 5;

The above line was just an example. This could be any any index number in your array. Define it, pass it, receive cgi input with the sort index. However.


Code
for my $key (sort {$hash{$a}[$sortbykey] <=> $hash{$b}[$sortbykey]} keys %hash

The above performs the sort on an array column, not the hash' keys, and returns an array (to the for loop) of the hash keys in the order of the sort results. But it still relies on knowing your data a bit. All of the entries in my sample were numbers, so I used the <=> comparison operator. If you want to hedge your bets, you can use:


Code
for my $key (sort{$hash{$a}[$sortbykey] cmp $hash{$b}[$sortbykey] 
|| $hash{$a}[$sortbykey] <=> $hash{$b}[$sortbykey]} keys %hash){

which will sort numerically, then "ascii"ily.

Basically, sorting is done before the first entry is worked on in the for loop, so in the for loop, all of the elements in the array is available.


Code
    # you can assign the values to a temp array 
my @array = @{$hash{$key}}; # @array now contains all of
# of the values in the row
print "@array";

# or, you can loop through the array
for my $val (@{$hash{$key}}){
print "$val ";
}

# or, you can access each element individually
print "$hash{$key}[0]\n";
print "$hash{$key}[1]\n";
print "$hash{$key}[2]\n"; # etc...

You may also want to take a look at my article, Hash Mania, at the Learning Center.

Hope this helps!



slauzier
Deleted

Feb 5, 2001, 2:17 PM

Post #5 of 6 (623 views)
Re: sorting a hash of an array [In reply to] Can't Post

ok, here is how i transformed what you suggested. it locks up my workstation when i try to run it, but it compiles fine. i think you might of misunderstood how i wanted to sort. My keys are my columns, and my indeces of the array inside that hash are the rows, so i have 9keys(columns) by 500+indeces(rows). I need to sort by the user-specified column(a.k.a., $contents{'field'}), then spit out, row by row all of the data based on the sort by the column that was selected. the trick is to preserve the integrity of the data structure. im having trouble getting my results to print with the correct rows/columns. here is a snippet of code: (i know this doesnt work, but i seem to be banging head against the wall here. i can kinda see where the logic just doesnt work, but i cant seem to fix it....its the perl newbie in me i guess)

Code
for my $key (sort $data{$contents{'field'}}[$a] <=> $data{$contents{'field'}}[$b]} keys Úta) { 

for ($i = 0; $i < $num_fields; $i++) {
print "<TH>$key_value[$i]";
}

for ($j = 0; $j < $num_rows; $j++) {
print "<TR>";
for ($i = 0; $i <= $num_fields; $i++) {
print "<TD>$data{$key_value[$i]}[$j]</TD>";
}
}
}



slauzier
Deleted

Feb 6, 2001, 3:08 PM

Post #6 of 6 (611 views)
Re: sorting a hash of an array [In reply to] Can't Post

is there some way that i could sort the array? i realize that this would slow everything down immensely, but i dont know of any other options. i have gotten this to not crash, but it doesnt seem to do anythign to the hash. (also, those two brackets that belong to the first for statement should not be there) ((for some reason i cant edit my post...time has expired))


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives