in reply to Searching data file
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 ' } };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Searching data file
by graff (Chancellor) on Nov 03, 2003 at 02:24 UTC | |
by Roger (Parson) on Nov 03, 2003 at 02:44 UTC | |
by Anonymous Monk on Nov 03, 2003 at 09:32 UTC |