Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hello gurus, Iam 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 for this which works as expected. Following 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 -> 15811 +38709 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'} };
EXPECTED output of Dumper (\%bibliography);
$VAR1 = { 'Lee2000a' => { 'abstract' => 'Abstract goes here', 'author' => 'Lee, Wenke and Stolfo, Salvatore J' 'title' => 'Data mining approaches for intrusion detec +tion' '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.

In reply to Dynamically parse BibTeX and create hash of hash by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (9)
    As of 2014-09-30 15:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (375 votes), past polls