2011-09-30 161 views
0

我有两个文件来自两个不同的服务器。在这两个文件中提供了一些橄榄球队的比赛。如你所知,橄榄球队可以用不同的名字命名。 我想实现一个代码,可以在两个文件中识别相同的足球比赛,以便从文件中获取相同的变量,而从另一个文件中获取其他变量。 例如在一个文件中,我有一个叫做比赛模式识别和字符串匹配

Derry City - Bray Wanderers 

,并在其他文件中我有一个叫做

Derry City - Bray 

我怎么能做到这一点同样的比赛? 我没有想法。

+3

说真的,**没有**的想法呢?甚至不完整,或错误或天真的? –

+0

唯一天真的想法是用搜索解析一个文件并用sed或perl替换子例程。但是对于每个新的团队,我都必须手动修复文件。 – emanuele

回答

1

非常简单的脚本来替换团队的别名。你需要自己填写别名,我做了一些。如果你有多个游戏,只要所有的别名交换完整的名字,哈希将覆盖现有的游戏。

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

my %games; 
while (<DATA>) { 
    chomp; 
    my ($home, $guest) = split /\s*-\s*/, $_, 2; 
    $home = get_name($home); 
    $guest = get_name($guest); 
    $games{"$home - $guest"} = 1; 
} 

sub get_name { 
# Return the full name for the team, if it exists, otherwise return the original 
    my %alias = (
     'Derry'  => 'Derry City', 
     'Brawlers' => 'Beijing', 
     'Dolphins' => 'Miami', 
     'Bray'  => 'Bray Wanderers', 
    ); 
    return $alias{$_[0]} // $_[0]; 
} 

use Data::Dumper; 
print Dumper \%games; 

__DATA__ 
Derry City - Bray Wanderers 
Derry City - Bray 
Brawlers - Dolphins 
Beijing - Miami 
Miami - Beijing 
1

在C++中:看看Boost.RegexBoost.Tokenizer,因为他们会做你所需要的。所有你需要的是一种模式来匹配。

boost::regex("Bray[\s]*(Wanderers)?", boost::regex::icase); 

或类似的东西 - 容易设置为一组单元测试。