ppremkumar has asked for the wisdom of the Perl Monks concerning the following question:
Team
I need help with fixing the below problem, for which I am unable to find a solution.
I am trying to write a program to extract all data within the tag "BIB."
The problem is this: When my find code is this
while ($data1 =~ m{(<BIB>.*</BIB>)}gx)the output comes as
<BIB>Falco (2012)</BIB> today Louise is hardly isolated. More than 5 m +illion babies have been born using the procedure, which has become al +most routine. And at the age of 28, Louise became a mother herself, g +iving birth to a baby boy name Cameron—conceived, by the way, in the +old-fashioned way (<BIB>Falco, 2012</BIB>; <BIB>ICMRT, 2012</BIB> Total occurrences of <BIB> is 1
which is not what I want.
When my find code is changed to this
while ($data1 =~ m{(<BIB>)}gx)I get something closer; at least the number of items within the "BIB" tag matches the total number of items within "BIB."
What I want is this, each entry saved as an array value:
<BIB>Falco (2012)</BIB>
<BIB>Falco, 2012</BIB>
<BIB>ICMRT, 2012</BIB>
use strict; use 5.14.2; my $bib_count = 0; my $INPUT_REF_FH; my @text_found; open $INPUT_REF_FH,"<:utf8", "ch01.txt"; binmode STDOUT, ':utf8'; while(<$INPUT_REF_FH>){ my $data1 = $_; while ($data1 =~ m{(<BIB>.*</BIB>)}gx){ $bib_count += 1; # print "$&\n"; push @text_found, ${^MATCH}; }; }; foreach (@text_found){ print "$_\n"; }; print "Total occurrences of <BIB> is $bib_count"; close $INPUT_REF_FH;
INPUT TEXT:
In fact, <BIB>Falco (2012)</BIB> today Louise is hardly isolated. More than 5 million babies have been born using the procedure, which has become almost routine. And at the age of 28, Louise became a mother herself, giving birth to a baby boy name Cameron—conceived, by the way, in the old-fashioned way (<BIB>Falco, 2012</BIB>; <BIB>ICMRT, 2012</BIB>).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extract Data between Tags
by tmharish (Friar) on Mar 05, 2013 at 18:23 UTC | |
by ppremkumar (Novice) on Mar 05, 2013 at 18:27 UTC | |
|
Re: Extract Data between Tags
by Your Mother (Archbishop) on Mar 05, 2013 at 19:05 UTC | |
by ppremkumar (Novice) on Mar 11, 2013 at 06:29 UTC |