How to put values from a look_down tree array's HTML tag into a regular array in Perl?

211 Views Asked by At

This is a snippet of code I've got:

#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use Time::Piece;
use HTML::Tree;

my $url0 = 'http://www.website.ch/blah.aspx';

my $doc0 = get($url0);

my $tree0 = HTML::Tree->new();
$tree0->parse($doc0);

my @juice = $tree0->look_down(
    _tag => 'option'
);

foreach ( @juice )
{
    print $_->as_HTML, "\n";
}

I understand that there are easier ways to do this--feel free to talk about those ways, but I'm doing it this way for now. I would like to put all the value entries into an array, so e.g. if one of my (what I'm calling) look_down tree array elements is the following

<option value="YIDDSH">Yiddish</option>,

then I would like to somehow push "YIDDSH" (without quotes) into an array, and the pull in the next value from the next element in the array.

1

There are 1 best solutions below

3
cjm On

The simplest way is to use the attr method to extract the contents of the value attribute, and the map function to loop over all the elements.

my @values = map { $_->attr('value') } @juice;