I have an array of hex numbers, and I need to go over other numbers and check if they appear in that array. Right now i'm using a foreach
loop that goes over the entire array each time. Is there a way to make it faster by sorting the array at first, and then implementing binary search on it.
The code at the moment:
sub is_bad_str{
my ($str, @keys) = @_;
my $flag = 0;
my ($key, $hex_num);
if ($str =~ m/14'h([0-9a-f][0-9a-f][0-9a-f][0-9a-f])/;){ #'# fixes bad highlighting
$hex_num = $1;
}
if (defined $hex_num){
foreach $key (@keys){
if ($hex_num =~ /Q$keyE/i){
$flag = 1;
last;
}
}
}
if (($flag == 0) && (defined $hex_num)){
return 1;#Bad str
}else{
return 0;#Good str
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…