perl中如何判断一个字符串是否在一个哈希中的值中出现,急急急

2025-05-01 03:02:29
推荐回答(1个)
回答1:

实在是看不太懂你的需求,想判断一个字符是否同时在哈希的键和对应键的值里面。

如果是的话,个人觉得类似的代码是:

#!perl
use strict;
use warnings;

my %hash = (
    # xxxx
);
my $file_name = 'xxxx';

open my $fh, '<', $file_name;

while (my $line = <$fh>) {
    # body...
    chomp $line;
    # ... do something with line

    for my $key (keys %hash) {
        # get hash value
        my $value = $hash{$line};

        # data exist in hash key and value
        if ( index($key, $line) != -1 and index($key, $value) != -1) {
            print "[$line] exist in hash key:[$key] and its value:[$value]$/";
            last;
        }
    }
}

close $fh;

__END__