The biggest time sink I see is that you look up $netHash{$val} 100 times in each loop.
Untested code, via top-of-my-head refactoring:
#!/usr/bin/perl use strict; use warnings; # Do you really need named hashes for later code? my @capVal_hashes = ( undef, \( my %capVal_1_Hash ), \( my %capVal_2_Hash ), \( my %capVal_3_Hash ), ... \( my %capVal_99_Hash ), \( my %capVal_100_Hash ), ); open IFH, '<', './chance.txt' or die; my %netHash; while (<IFH>) { my ( $key, $val ) = split; next if not $val > 1; my $hash_num = ( $val <= 1.0 ) ? undef : ( $val < 1.4 ) ? 1 : ( $val == 1.4 ) ? undef # Intentional gaps? : ( $val < 1.9 ) ? 2 : ( $val == 1.9 ) ? undef # Intentional gaps? ... ... : ( $val < 99.2 ) ? 98 # XXX Assumed : ( $val == 99.2 ) ? undef # Intentional gaps? : ( $val < 99.8 ) ? 99 : ( $val <= 100.1 ) ? undef # Intentional gaps? : ( $val < 100.9 ) ? 100 : undef ; if ( defined $hash_num ) { $capVal_hashes[$hash_num]{$key} = $val; } } close IFH or warn; foreach my $href ( @capVal_hashes ) { next if not defined $href; my @keys = keys %{ $href }; my $random_key = @keys[ rand @keys ]; my $random_val = $href->{$random_key}; print "$random_key $random_val\n"; }
In reply to Re: better way to printing random hash key and its value
by Util
in thread better way to printing random hash key and its value
by garbage777
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |