And the output is as expected -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>
$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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |