动态解析BibTeX并创建哈希散列表

4

我正在尝试解析以下BibTeX文件(bibliography.bib):

@book{Lee2000a,
abstract = {Abstract goes here},
author = {Lee, Wenke and Stolfo, Salvatore J},
title = {{Data mining approaches for intrusion detection}},
year = {2000}
}
@article{Forrest1996,
abstract = {Abstract goes here},
author = {Forrest, Stephanie and Hofmeyr, Steven A. and Anil, Somayaji},
title = {{Computer immunology}},
year = {1996}
}

我正在使用BibTeX::Parser包,它的工作很正常。但是在创建哈希结构时遇到了问题。以下是我的代码:
#!/usr/bin/perl
# http://search.cpan.org/~gerhard/BibTeX-Parser-0.62/lib/BibTeX/Parser.pm
use BibTeX::Parser;
use IO::File;
use Data::Dumper;
use strict;
use warnings;

my $filename="bibliography.bib";
my (%bibliography, %article);
my $i;
my ($entry, @entries, $type, $key);
my (my $hkey, my $hvalue);

# open BibTeX
my $fh = IO::File->new("$filename") or die "could not open $filename: $!\n";

# create parser object ...
my $parser = BibTeX::Parser->new($fh);

# ... and iterate over entries
while ($entry = $parser->next ) {
  if ($entry->parse_ok) {

    # return BibTeX elements like abstract, author, title ...
    @entries = $entry->fieldlist();

    # create %article as a hash array e.g. year -> 1996; isbn -> 1581138709 etc.
    foreach (@entries) {
      $article{"$_"} = $entry->field("$_");
    }

    # return article's key (Lee2000a, Forrest1996)
    $key = $entry->key;

    # append %article into %bibliography with approporiate key
    $bibliography{"$key"} = \%article;

    #Debug
    #print $entry->key, "\n";
    #print Dumper (\%article);

    # removes all elements of %article (prepare for next iteration)
    %article = ();

    #Debug
    #print "================================\n";
  }

  else {
    warn "Error parsing file: " . $entry->error;
 }
}

    #Debug
    #print Dumper (\%bibliography);

Dumper(\%bibliography) 的当前输出:

$VAR1 = {
          'Lee2000a' => {},
          'Forrest1996' => $VAR1->{'Lee2000a'}
        };

Dumper(\%bibliography)的期望输出:

$VAR1 = {
          'Lee2000a' => {
                'abstract' => 'Abstract goes here',
                'author' => 'Lee, Wenke and Stolfo, Salvatore J'
                'title' => 'Data mining approaches for intrusion detection'
                'year' => '2000'
              },
          'Forrest1996' => {
                'abstract' => 'Abstract goes here',
                'author' => 'Forrest, Stephanie and Hofmeyr, Steven A. and Anil, Somayaji'
                'title' => 'Computer immunology'
                'year' => '1996'
                }
        };

我做错了什么?非常感谢。
2个回答

2

尝试不包含这行代码运行:

# removes all elements of %article (prepare for next iteration)
%article = ();

你已将 $bibilography{$key} 设置为对该哈希的引用,然后你又将其清空。
此外,在循环内部移动你对 %article 的声明(可能是在 if ($entry->parse_ok) { 之后),这样它的范围就局限于你使用它的地方了,不需要重新初始化。
希望这可以帮到你……
更新以包括排序问题…… 这应该可以对你的哈希进行排序:
foreach my $bib_key ( sort keys %bibliography ) {
  print "$bib_key\n";

  foreach my $article_key (sort keys %{ $bibliography{$bib_key} }) {
    print "\t $article_key: $bibliography{$bib_key}{$article_key}\n";
  }
}

谢谢你们两个,你们帮了我很多。你们能否建议一下我如何首先按照"%bibliography"哈希键(例如Forrest1996、Lee2000a)对这个结构进行排序,然后再按照"%article"哈希键(例如作者、摘要、标题、年份等)进行排序呢?我尝试过类似这样的代码,但没有成功:for $i (sort keys(%bibliography)){ print "$i", "\n"; for $j (sort keys ($i)){ print "$j\n"; } } - Wakan Tanka

1

Dumper输出

$VAR1 = { 'Lee2000a' => {}, 'Forrest1996' => $VAR1->{'Lee2000a'} };

显示您的哈希表正在共享结构,即$bibliography->{Lee2000a}$bibliography->{Forrest1996}是对同一篇文章哈希表的引用。您的代码在外部作用域有my %article,并且每次循环迭代都会清除并重新填充此共享哈希表。

相反,您希望每次迭代都创建一个新的内部文章哈希表。删除外部的%article并将其移入循环中-如下所示标记为(+)。删除%article = ()行,这将破坏您刚收集的数据。

while ($entry = $parser->next) {
  if ($entry->parse_ok) {
    # return BibTeX elements like abstract, author, title ...
    @entries = $entry->fieldlist();

    # create %article as a hash array e.g. year -> 1996; isbn -> 1581138709 etc.
    my %article;  # (+)
    foreach (@entries) {
      $article{$_} = $entry->field($_);
    }

    # return article's key (Lee2000a, Forrest1996)
    $key = $entry->key;

    # insert %article into %bibliography with appropriate key
    $bibliography{$key} = \%article;
  }
  else {
    warn "Error parsing file: " . $entry->error;
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接