Find the answer to your Linux question:
Results 1 to 2 of 2
Hello gurus, Iam trying to parse following BibTex file (bibliography.bib): Code: @book{Lee2000a, abstract = {Abstract goes here}, author = {Lee, Wenke and Stolfo, Salvatore J}, title = {{Data mining approaches ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    70

    Dynamically parse BibTeX and create hash of hash


    Hello gurus, Iam trying to parse following BibTex file (bibliography.bib):

    Code:
    @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 for this which works as expected, problem that I have is with creating hash of hash array. Following code:

    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);
    Code:
    $VAR1 = {
              'Lee2000a' => {},
              'Forrest1996' => $VAR1->{'Lee2000a'}
            };

    EXPECTED output of Dumper (\%bibliography);
    Code:
    $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.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hi,

    Try this modified loop:
    Code:
    while($entry = $parser->next ){
      if($entry->parse_ok){
    
        # return article's key (Lee2000a, Forrest1996)
        $key = $entry->key;
    
        # 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("$_");
          $bibliography{$key}{$_} = $entry->field($_);
        }
      }else{
        warn "error...";
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •