2014-02-11 31 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"; 
} 
相關問題