Question

I'm trying to parse following BibTeX file (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}
}

I am using BibTeX::Parser package which works as expected, problem is with creating hash of hash structure. This is my code:

#!/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);

CURRENT output of Dumper (\%bibliography):

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

DESIRED output of 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'
                }
        };

What I am doing Wrong ? Many thanks.

Was it helpful?

Solution

Try your code without this line:

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

You've set $bibilography{$key} as a reference to that hash, and then you're emptying it out.

Also, move your declaration of %article inside your loop (maybe right after if ($entry->parse_ok) {, so that its scope is local to where you are using it, and re-initializing it wont be necessary.

Hope that helps...

Updating to include sorting question... this should work to sort your hash:

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";
  }
}

OTHER TIPS

The Dumper output

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

shows that your hash is sharing structure, i.e., $bibliography->{Lee2000a} and $bibliography->{Forrest1996} are references to the same article hash. Your code has my %article at an outer scope, and each iteration of your loop cleans out and repopulates this shared hash.

Instead, you want each iteration to create a new inner article hash. Remove the outer %article and move it into the loop—marked (+) below. Delete the %article = () line, which would destroy the data you just collected.

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;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top