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: Advanced:
Loop only throw part of Hash // prevent sort problems

 



eagle
New User

May 23, 2010, 3:23 AM

Post #1 of 5 (2353 views)
Loop only throw part of Hash // prevent sort problems Can't Post

Hi all,

I tried to find a solution by extensive googling, but didn't get a good solution so far.

Problem: I run throw big files and save hits in a big hash file. Acccording to the files name, they are saved as %bighash{hitname}{filename} whereas scores are subclassed as %bighash{hitname}{filename}->{score}.

This means that lateron, I get a big hash with 'hitname' entries that do not contain entries for all the files. I wanna sort then the enrtries coming from only one file by using a code as follows:

Code
	 foreach $value (sort {$targets{$b}{'filename'}->{'aggpval'} <=> $targets{$a}{'filename'}->{'aggpval'} } keys %targets )      { 
$j++;
if ( $j <= 50 ) {
print "$j: $value ". $targets{$value}{'filename'}->{'aggpval'} ."\n";
}
}

The problem is then that for some keys of the hash, the subhash is not defined and perl grumbles since the sort is supposed to sort non defined values. Is there a way to exclude the not defined parts of the hash properly?

ideally, i would like to have a sort that would follow this logics:
foreach $value ( sort ... ) keys %hash{.*}{'filename'}

i found some hints about hash slices etc, but i would like to keep all the entries within one big hash for different reasons...

(by the way, i know that i should rather leave the loops when reaching 50 instead of looping throw the whole thing)

thanks a lot for any help


shawnhcorey
Enthusiast


May 23, 2010, 4:45 AM

Post #2 of 5 (2351 views)
Re: [eagle] Loop only throw part of Hash // prevent sort problems [In reply to] Can't Post

Use a Schwartzian Transformation to sort. That what your sort key expression is something like:

Code
$targets{$}{'filename'}->{'aggpval'} || 0


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


eagle
New User

May 23, 2010, 12:08 PM

Post #3 of 5 (2343 views)
Re: [shawnhcorey] Loop only throw part of Hash // prevent sort problems [In reply to] Can't Post

hi,
thanks for your help. i tried your hint out, but did not get it working inside the sort syntax... even after googling for the Schwartz transformation. do you talk about putting it inside my code, or in a isolated loop? could you help me to precise your solution?
my only aim is to get rid of the comparisons for the keys where the value to be compared is not defined...


shawnhcorey
Enthusiast


May 23, 2010, 12:49 PM

Post #4 of 5 (2341 views)
Re: [eagle] Loop only throw part of Hash // prevent sort problems [In reply to] Can't Post


Code
my @sorted_keys = map { $_->[0] } 
sort { $b->[1] <=> $a->[1] }
map { [ $_, $targets{$_}{'filename'}->{'aggpval'} || 0 ] }
keys %targets;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


BillKSmith
Veteran

May 24, 2010, 3:20 PM

Post #5 of 5 (2294 views)
Re: [shawnhcorey] Loop only throw part of Hash // prevent sort problems [In reply to] Can't Post

You seem to have solved the stated problem, but I suspect that the "missing" data will be a problem later in the program. Do you see a problem with using grep (before the sort) to select only those keys which do have data for the current file? I am not prepared to recommend this without a second opinion.
Good Luck,
Bill

 
 


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

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