CoVAX has asked for the wisdom of the Perl Monks concerning the following question:
Note: After composing this question I realized I could use just one array to store references to anonymous arrays containing the three fields I'm interested in. Still, I'm curious to learn one or more ways to accomplish my original question.
In the code below, what is the "Perl way" to simultaneously push $1, $2, and $3 to the three arrays @ids, @urls, and @titles? That is, I'd like to learn a way to do this with just one assignment (unless the code would violate the Simplicity & Clarity (from "The Practice of Programming" by Kernighan and Pike).
Caveat: In actual use this code will be extracting about 20 sets of url/id/title from a scalar variable holding the contents of an approx. 250kb HTML file fetched via LWP. Which means: the regex will include the 'g' modifier so the 'while' loop won't be present.
#! perl -w use strict; use Data::Dump qw(dump); my (@urls, @ids, @titles) = (); while (<DATA>) { my ($url, $id, $title) = ($_ =~ m|<h6 class="project-title"><a hre +f="(/projects/(\d+)/.+?\?ref=discovery)" target="">(.+?)</a></h6>|o ) +; push @urls, $url; push @ids, $id; push @titles, $title; } dump @urls, @ids, @titles; __END__ <h6 class="project-title"><a href="/projects/1956727289/how-to-build-a +-spaceship?ref=discovery" target="">How To Build A Spaceship</a></h6> +<p class="project-byline">Rohan Sinha</p>
Thank you for your time.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: What is Perl way to simultaneously assign to three separate arrays?
by LanX (Saint) on Feb 15, 2015 at 03:03 UTC | |
by CoVAX (Beadle) on Feb 15, 2015 at 04:41 UTC | |
by LanX (Saint) on Feb 15, 2015 at 10:42 UTC | |
by CoVAX (Beadle) on Feb 15, 2015 at 20:40 UTC | |
by CoVAX (Beadle) on Feb 15, 2015 at 08:42 UTC | |
|
Re: What is Perl way to simultaneously assign to three separate arrays?
by LanX (Saint) on Feb 15, 2015 at 03:30 UTC |