1

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.

tjd
  • 4,064
  • 1
  • 24
  • 34
user3333975
  • 125
  • 10

1 Answers1

3

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;
cjm
  • 61,471
  • 9
  • 126
  • 175
  • Can you match and replace in the same line? That is, what if I have `value="PIG&PORK S"`, and I want to replace every instance of `&` with `#` and every instance of ` ` with `0`. – user3333975 Mar 03 '14 at 18:51
  • 1
    `map { my $s = $_->attr('value'); $s =~ s/&/#/g; $s =~ s/ /0/g; $s }` – ikegami Mar 03 '14 at 19:03
  • 1
    `map { $_->attr('value') =~ s/&/#/rg =~ s/ /0/rg }` (requires 5.14+) – ikegami Mar 03 '14 at 19:04