比较字符串与哈希映射perl中的键值对 [英] comparison of string against key-value pair in hash map perl

查看:376
本文介绍了比较字符串与哈希映射perl中的键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Perl的哈希映射中有键值对。假设所有密钥都是唯一的。



例如:

  my%msg_to_number =(
'Hello World,我是XYZ'=> 11,
'我使用堆栈溢出进行指导'=> 12,
'编程很好!'=> 13,
);

现在如果我想比较的输入字符串如下所示:

  str1 = Hello World,我是XYZ; 
str2 = Hello World,我是XYZ和ABC;

所以下面的代码将str1正确映射到哈希映射关键字,但是对于str2而言
失败。 / p>

我的问题是如何修改下面的代码以使它适用于两种情况。即:使代码适用于str1和str2。对于str1和str2,哈希映射应该返回11。这甚至在哈希映射关键字匹配字符串的一部分或比较完整的字符串时应该返回匹配。 (我假设部分匹配的情况会出现在句子的开头与单词相比较,这简化了一下)



现在下面的代码通过删除字符进行比较像!,#等等,转换为小写,然后匹配。

 #!/ usr / bin / env perl 
use strict;
使用警告;

my%msg_to_number =(
'Hello World,我是XYZ'=> 11,
'我正在使用Stack Overflow进行指导'=> 12,
'编程很好!'=> 13,
);

my $ str_to_match ='Hello World,我是XYZ !!!!!';
my $ transformed_match = $ str_to_match =〜s / \W // gr;
$ b $ my($ first_match)= grep {s / \W // gr =〜m / ^ \ Q $ transformed_match\E $ / i}键
%msg_to_number;
print$ first_match = $ msg_to_number {$ first_match} \\\
;

我曾尝试使用上述代码的正则表达式,但无法使其正常工作。如果有人可以提出一些改变或不同的方法(建议)做同样的事情会很好。 (代码正在进行的原始逻辑加上部分比较)。这是堆栈溢出的后续问题。

谢谢

更新:
应该匹配什么和不应该匹配的示例。

下面假设哈希映射:
my%msg_to_number =(
'Hello World,我是XYZ'=> 11,
'我是使用堆栈溢出进行指导'=> 12,
'编程很好!'=> 13,
);

  str1 = Hello World,我是XYZ 
str2 = Hello World
str3 = Hello World,我是XYZ,ABC和EFG。

所以在上面的str1和str2中应该匹配
,而str3不匹配。



正如我所说,即使起始部分是部分匹配,它应该匹配。



让我知道如果这清除了使用case

解决方案

这可能很简单:

  my($ first_match)= grep {s / \W // gr =〜m / \ $ $ transformed_match\E / i}键%msg_to_number; 

删除模式定位符,并提供 $ transformed_match 是(已转换)键的子字符串,则匹配。



或者您可以反转 - 如果是子字符串,则匹配:

 #!/ usr / bin / env perl 
use strict;
使用警告;

my%msg_to_number =(
'Hello World,我是XYZ'=> 11,
'我正在使用Stack Overflow进行指导'=> 12,
'编程很好!'=> 13,
);

my $ str_to_match ='Hello World,我是XYZ和ABC !!!!!';
my $ transformed_match = $ str_to_match =〜s / \W // gr;

my($ first_match)= grep {my $ tr_key = s / \W // gr; $ transformed_match =〜m / $ tr_key / i或$ tr_key =〜m / $ transformed_match /}键%msg_to_number;
print$ first_match = $ msg_to_number {$ first_match} \\\
;

(可能有一种方法可以在正则表达式中进行变换和匹配 - 我是不是100%肯定,但它可能不是一个好主意!)


I have key-value pairs in a hash map in Perl. Assuming all keys are unique.

for example like this :

my %msg_to_number = ( 
 'Hello World, I am XYZ'    => 11,
 'I am using Stack Overflow for Guidance'   => 12,
 'Programming is good!' => 13,
);

now if input strings against which I want to compare is like this:

str1 = Hello World, I am XYZ;
str2 = Hello World, I am XYZ and ABC;

so below code maps str1 to hash map key correctly but for str2 it fails.

My question is how can i modify below code to make it working for case both cases. That is : making code work for str1 and str2 as well. Hash map should return 11 for both str1 and str2. That is even key in hash map matches a part of string in comparison or complete string it should return match. (I am assuming partial match case will occur with words in beginning of sentence to be compared against, this simplifies things a bit)

right now below code makes comparison by removing characters like !, # and so on, converting to lower case and then match.

#!/usr/bin/env perl
use strict;
use warnings;

my %msg_to_number = ( 
 'Hello World, I am XYZ'    => 11,
 'I am using Stack Overflow for Guidance'   => 12,
 'Programming is good!' => 13,
);

my $str_to_match = 'Hello World, I am XYZ!!!!!';
my $transformed_match = $str_to_match =~ s/\W//gr;

my ( $first_match ) = grep { s/\W//gr =~ m/^\Q$transformed_match\E$/i } keys     
%msg_to_number;
print "$first_match   =  $msg_to_number{$first_match}\n";

I have tried playing with regex for above code but was not able to make it work. if someone can suggest some changes or different method( suggestion )to do same will be great. ( original logic which code is doing currently plus partial comparison ). This is a follow up question on stack overflow.

Thanks

Updated: Example of what should match and what should not match.

Assume below hash map: my %msg_to_number = ( 'Hello World, I am XYZ' => 11, 'I am using Stack Overflow for Guidance' => 12, 'Programming is good!' => 13, );

str1 = Hello World, I am XYZ
str2 = Hello World
str3 = Hello World, I am XYZ, ABC and EFG.

so in above str1 and str2 should match whereas str3 no match.

As i said even if starting part is partial match then it should be match.

let me know if this clears the use case

解决方案

This may be as simple as:

my ( $first_match ) = grep { s/\W//gr =~ m/\Q$transformed_match\E/i } keys %msg_to_number;

Remove the pattern anchors, and provided $transformed_match is a substring of the (transformed) key, then it'll match.

Or you can reverse that - so if the key is the substring, it matches:

#!/usr/bin/env perl
use strict;
use warnings;

my %msg_to_number = ( 
 'Hello World, I am XYZ'    => 11,
 'I am using Stack Overflow for Guidance'   => 12,
 'Programming is good!' => 13,
);

my $str_to_match = 'Hello World, I am XYZ and ABC!!!!!';
my $transformed_match = $str_to_match =~ s/\W//gr;

my ( $first_match ) = grep { my $tr_key = s/\W//gr; $transformed_match =~ m/$tr_key/i or $tr_key =~ m/$transformed_match/ } keys %msg_to_number;
print "$first_match   =  $msg_to_number{$first_match}\n";

(There may be a way to do the transform-and-match within the regex - I'm not 100% sure. But it's probably not a great idea anyway!)

这篇关于比较字符串与哈希映射perl中的键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆