1

我有一个名为“lookup”的子程序,它在哈希中查找给定值。我意识到如果我可以要求它不查找给定值,而是查找比作为参数传递的值更小的值,它会更强大。

我可以让lookupbigger、lookupsmall..等,但我相信有更好的方法。

# lookup id according to the search criteria
sub lookup {
  my( $data, $lk, $lv ) = ( @_ );
  my @res;

  foreach my $key (keys $data) {
    my $value = $$data{$key};
    next unless( defined $$value{$lk} );
    # this is the line where I want to replace eq with another operator
    push(@res, $key) if( $$value{$lk} eq $lv );
  }

  return \@res;
}
4

3 回答 3

6

您可以将标准函数传递给查找函数:

#!/usr/bin/env perl

use strict; use warnings;
use YAML;

my %hash = qw(a 1 b 2 c 3 d 4 e 5);

# find all keys with odd values

print Dump lookup_keys_by_value(\%hash, sub {
        return unless @_;
        my $v = shift;
        return $v % 2;
    },
);

sub lookup_keys_by_value {
    my ($hash, $criterion) = @_;

    my @keys;

    while (my ($k, $v) = each %$hash) {
        push @keys, $k if $criterion->($v);
    }

    return \@keys;
}
于 2012-05-03T11:12:07.017 回答
2

这是一个想法(也许太“聪明”):

use strict;
use warnings;

{   no strict 'refs';
    # When called like __PACKAGE__->$op( ... ),  __PACKAGE__ is $_[0]
    *{'>'}  = sub { return $_[1] > $_[2]; }; 
    *{'<'}  = sub { return $_[1] < $_[2]; };
    *{'=='} = sub { return $_[1] == $_[2]; };
}

sub determine {
    my ( $first_arg, $op, $second_arg ) 
        = map { s/^\s+//; s/\s+$//; $_ }
          split( /\s*([<>]|==)\s*/, @_ == 1 ? shift : "@_" )
        ;
    say "$first_arg $op $second_arg => " 
      . (  __PACKAGE__->$op( $first_arg, $second_arg ) ? 'TRUE' : 'FALSE' )
      ;
}

determine( qw( 1 < 2 ) );
determine( qw( 2 < 1 ) );
determine( qw( 1 > 2 ) );
determine( qw( 2 > 1 ) );
determine( qw( 1 == 2 ) );
determine( qw( 1 == 1 ) );
determine( qw( 2 == 2 ) );
determine( ' 1 < 2 ' );
determine( ' 2 < 1 ' );
determine( ' 1 > 2 ' );
determine( ' 2 > 1 ' );
determine( ' 1 == 2 ' );
determine( ' 1 == 1 ' );
determine( ' 2 == 2 ' );
于 2012-05-03T13:20:32.850 回答
1

您可以尝试使用重载

use overload (
'>' => 'compareBigger',
'<' => 'compareSmaller',
'==' => 'equals'
 )
于 2012-05-03T10:44:59.603 回答