【问题标题】:Perl: find the team of a player. Is there a more elegant way?Perl:找到一个球员所在的球队。有没有更优雅的方式?
【发布时间】:2014-02-11 13:22:23
【问题描述】:

我在一个文件 teams.txt 中有几个团队,如下所示:

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

我可以根据自己的喜好更改文件格式。

现在我需要找出一个球员,比如鲍勃,属于哪支球队。 到目前为止,我使用的是open file -> while -> match pattern

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

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

【问题讨论】:

    标签: perl config


    【解决方案1】:

    如果这是一个真实的任务并且文件很大,我会使用 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";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      • 2016-09-18
      • 2019-07-10
      • 1970-01-01
      相关资源
      最近更新 更多