nascent has asked for the wisdom of the Perl Monks concerning the following question:

How do I pass the source of a URL to an array? For example, pass CNN.com's source to @source so I can regex it for a certain string?

Thanks,
nascent

Replies are listed 'Best First'.
Re: URL's
by chromatic (Archbishop) on Mar 27, 2000 at 07:50 UTC
    Why an array? I'm not aware of any way to apply a regex to an array as a whole (as opposed to iterating over each element and applying the regex individually).

    If you're determined to do it, though, use LWP::Simple to get the web page and use split with some value to break up the scalar into a list, assigning it to your array. (I still think you might as well use the regex on the scalar and skip the array part.)

      Okay, let me rephrase.

      I have a URL whose source contains a stock price. It always comes after the only "last:" in the entire page. How can I tell perl to print whatever comes after that?

      nascent

        Note that btrott's solution will assign an empty string to $source if the pattern doesn't match. You will need to test for that at some time. I prefer a regex like this:
        my $quote = ""; if ($source =~ /last: ([0-9.]+)/i;) { $quote = $1; } else { # do the Not Found Dance }
        It's a matter of personal preference -- just don't neglect the check! Note also that the regex may run faster without the .* at the start (backtracking can be expensive).
        Try something like:
        my($quote) = $source =~ /.*last: ([0-9.]+)/i;
        where $source contains the source of the HTML page. You may need to tweak this a bit to match exactly what you want (for example, you want to match what comes after "last: "-- but how much do you want to match after that? The quote, which should, I guess, be a number?)
Re: URL's
by japhy (Canon) on Mar 27, 2000 at 23:37 UTC
    Why use a regex on the whole file? Find the position of the text "last:" using index(), and then use a regex, on a small substring of the file starting at that particular offset.