I have rewritten your code with a simpler algorithm.
use strict; use Data::Dumper; my %records; # hash to store book info based on author my $data; while (<DATA>) { chomp; $data .= $_; if ($_ eq '</ref>') { # process what's in the buffer when we see the end tag my $rec = process_record($data); $records{$rec->{author}} = $rec; $data = ''; } } print print Dumper(\%records); sub process_record { my $rec = shift; my %col; ($col{author}) = $rec =~ m/<author>\s*([^<]*)(?=<)/g; ($col{year}) = $rec =~ m/<year>\s*([^<]*)(?=<)/g; ($col{source}) = $rec =~ m/<source>\s*([^<]*)(?=<)/g; ($col{id}) = $rec =~ m/<id>\s*([^<]*)(?=<)/g; ($col{title}) = $rec =~ m/<title>\s*([^<]*)(?=<)/g; my @keywords = $rec =~ m/<key>\s*([^<]*)(?=<)/g; $col{keywords} = \@keywords; return \%col; } __DATA__ <ref> <provnc> <aulist> <author> Bin Laden </aulist> <year>1990 <source> Cambridge University Press, Cambridge UK, 1st edition <id>1 <keywords> <key>terrorism <key>whatever </keywords> </provnc> <title> Terrorism </ref> <ref> <provnc> <aulist> <author> Sydney </aulist> <year>1990 <source> Cambridge University Press, Cambridge UK, 1st edition <id>1 <keywords> <key>nothing <key>whatever </keywords> </provnc> <title> Terrorism </ref>
And the output is as expected -
$VAR1 = { 'Bin Laden' => { 'title' => 'Terrorism', 'author' => 'Bin Laden', 'keywords' => [ 'terrorism', 'whatever' ], 'id' => '1', 'year' => '1990', 'source' => 'Cambridge University Press, Ca +mbridge UK, 1st edition ' }, 'Sydney' => { 'title' => 'Terrorism', 'author' => 'Sydney', 'keywords' => [ 'nothing', 'whatever' ], 'id' => '1', 'year' => '1990', 'source' => 'Cambridge University Press, Cambr +idge UK, 1st edition ' } };

In reply to Re: Searching data file by Roger
in thread Searching data file by parisa

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.