0

I have an array of elements which are basically HTML tags. Below, is an example

<L>
 <LI>
  <LI_Label>Label1</LI_Label>
  <LI_Title>Title1</LI_Title>  
 </LI>
  <LI>
  <LI_Label>Label2</LI_Label>
  <LI_Title>Title2</LI_Title>  
 </LI>
 <LI>
  <LI_Label>Label3</LI_Label>
  <LI_Title>Title3</LI_Title>  
 </LI>
</L>

I am trying to extract only the LI_Title elements and store them into a separate array which I want to then concatenate into 1 complete string. For extract and store, I am using the below script. However, when I print the array, the entire block of HTML is in the Found_LI array and not just the LI_Title elements as I am expecting. Hoping someone here can point out what i am doing wrong below?

foreach (@po_siblings)
{
    if ($_ =~ /LI_Title/)
    {
        push(@found_LI,$_);
    }
}
print "@found_LI\n";
3
  • The problem is that @po_siblings does not contain what you think it does. You think it's an array with one element per line, but its elements are actually bigger than that. (Maybe even the whole thing is just a single element?) Commented Jul 7, 2013 at 16:32
  • And the simplest fix is to replace your entire if statement with something like push @found_LI, m/<LI_Title>.*?<\/LI_Title>/g. Commented Jul 7, 2013 at 16:33
  • 1
    I suggest using an HTML parser. stackoverflow.com/questions/4598162/html-parsing-in-perl Commented Jul 7, 2013 at 16:47

1 Answer 1

1

As your sample "html" is in fact well-formed XML — why not use an XML parser and find the nodes and values using XPath queries? Here's a sample script to solve your problem using XML::LibXML:

use strict;
use XML::LibXML;

my $blob = <<'EOF';
<L>
 <LI>
  <LI_Label>Label1</LI_Label>
  <LI_Title>Title1</LI_Title>  
 </LI>
  <LI>
  <LI_Label>Label2</LI_Label>
  <LI_Title>Title2</LI_Title>  
 </LI>
 <LI>
  <LI_Label>Label3</LI_Label>
  <LI_Title>Title3</LI_Title>  
 </LI>
</L>
EOF

my $p = XML::LibXML->new;
my $doc = $p->parse_string($blob);
print join(" ", map { $_->textContent } $doc->findnodes('/L/LI/LI_Title')), "\n";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.