2014-02-11 29 views
1

我有一个文件teams.txt几支球队看起来像:Perl:找到一个球员的球队。有没有更优雅的方式?

team1 = {bob marc steve} 
team2 = {john maria} 

文件格式,我可以根据我的口味改变。

现在我需要找出一个球员,说bob,属于哪个球队。 到目前为止,我正在使用open file -> while -> match pattern

我不知道我是否可以重新格式化文件teams.txt并使用Config :: General或 cpan中的任何其他先入为主的工具。

请问我会享受很多新的想法! (使用perl 5)

回答

0

如果它是一个真正的单词任务和文件大,我会使用SQLite和一些基本的SQL查询来获取此信息。

讨厌,不过很简单:

my $player = 'john'; 
#return only the team names 
my @found = `grep -w $player file1 file2 | cut -f1 -d"="`; 
chomp(@found); 
if (@found){ 
    print "We found $player in teams: ".join(',',@found)."\n"; 
} 
相关问题