in reply to Re: Regular Expression Help
in thread Regular Expression Help

Hi haukex, thank you for your insights. I used the DOM and I get the following results for https://finance.yahoo.com/quote/XOM?p=XOM&.tsrc=fin-srch
==> <span class="Trsdu(0.3s) Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(b)" + data-reactid="14">43.88</span> <== ==> <span class="Trsdu(0.3s) Fw(500) Fz(14px) C($positiveColor)" data- +reactid="16">+0.15 (+0.34%)</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="68">43.73</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="73">43.59</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="78">43.79 x 900</span> <= += ==> <span class="Trsdu(0.3s) " data-reactid="83">43.80 x 900</span> <= += ==> <span class="Trsdu(0.3s) " data-reactid="96">19,803,615</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="101">38,560,761</span> <= += ==> <span class="Trsdu(0.3s) " data-reactid="109">185.631B</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="114">1.27</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="119">13.07</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="124">3.36</span> <== ==> <span class="Trsdu(0.3s) " data-reactid="143">47.13</span> <==
My questions:
sub { print "==> $_ <==\n" }
How do I capture the $_ in a variable for example if I do this
sub {push @myArray, $_; }
It fails! I am trying to understand how I can capture the output in your sub into an array.

Replies are listed 'Best First'.
Re^3: Regular Expression Help
by haukex (Archbishop) on Apr 27, 2020 at 20:31 UTC
    sub {push @myArray, $_; } It fails! I am trying to understand how I can capture the output in your sub into an array.

    pushing the results onto an array works fine for me, so you'd have to show an SSCCE of how it's failing for you. However, note that you don't need to create a new array - the return value of the ->find method is a Mojo::Collection object, which is basically just a fancy array reference. In other words, you can do my $c = $dom->find(...) and then @$c is the array of results - an array of Mojo::DOM objects. Just a guess, but perhaps you need to look at those docs to see what you can do with such objects, such as for example calling their ->all_text method to get their contents. (You can use Perl's grep, map, and other array operations on @$c, or you can use Mojo::Collection's methods such as $c->grep(...) and $c->map(...), which have a different API and return Mojo::Collection objects. ->each is basically the object's version of Perl's foreach.)