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

Hi Monks I'm new here hope you could help me with this..

I recently searched for a code that will read or get the contents of an html file so when I run the code it displayed the html code on the cmd screen.. My problem is how can I show the words that I want to see on the screen... I used grep on the code and there I inserted the word that what I want to appear on cmd.. When I run the code it just displayed number 1 on the screen.. For what I've understand it means that there is a sentence like that based on the grep pattern.

use strict; use LWP::Simple; use LWP::UserAgent; my $ua = new LWP::UserAgent; $ua->timeout(120); my $url='http://www.gamezebo.com/games/marvel-avengers-alliance/walkth +rough'; my $request = new HTTP::Request('GET', $url); my $response = $ua->request($request); my $content = $response->content(); my @content = "$content"; my $introduction = grep (/Game Introduction - Marvel: Avengers Allianc +e/, @content); print "$introduction"; exit 0;

Instead of 1 how can I make that sentence appear? Thank you so much...

  • Comment on How to output the words that you want that came thru an html file?
  • Download Code

Replies are listed 'Best First'.
Re: How to output the words that you want that came thru an html file?
by jwkrahn (Abbot) on May 04, 2012 at 05:43 UTC
    my $content = $response->content(); my @content = "$content"; my $introduction = grep (/Game Introduction - Marvel: Avengers Allianc +e/, @content); print "$introduction";

    That is the same as saying:

    my $content = $response->content(); my @content; $content[ 0 ] = $content; my $introduction = grep( /Game Introduction - Marvel: Avengers Allianc +e/, $content[ 0 ] ); print $introduction;

    Or:

    my $content = $response->content(); my $introduction = grep( /Game Introduction - Marvel: Avengers Allianc +e/, $content ); print $introduction;

    Or more properly:

    my $content = $response->content(); my $introduction = $content =~ /Game Introduction - Marvel: Avengers A +lliance/; print $introduction;

    In other words, you don't have a list of "lines" in @content, just a single string.

    You may want something like:

    my @introduction = grep /Game Introduction - Marvel: Avengers Alliance +/, split /^/, $response->content(); print @introduction;
      Yes, but how am I going to remove the h2 and /h2 tags?
        Looks like you need a HTML::Parser module.

        If that is too much, you can start with code like this:

        my @content = split /^/, $response->content(); my $introduction; for my $line (@content){ next unless ( $introduction ) ||= $line=~/Game Introduction - Marvel +: Avengers Alliance/; my $EndSection = $line=~/<\/section>/; local $_ = $line; # Make a copy, so we do not modify @content.. m|^\s*<[^/>]+>(.+)</| and $_=$1; # Zap tags on both sides, if any s|<[^>]+>||g; # Zap single </onetag> tags print; last if $EndSection ; }
        which , I suspect , is close to what you are looking for.

                     I hope life isn't a big joke, because I don't get it.
                           -SNL

        My favourite is HTML::TokeParser. This is a code fragment that finds the next <td> tag and then gets the text in the data cell.
        $stream = HTML::TokeParser->new( \$content ) $stream->get_tag('td'); $key = $stream->get_trimmed_text('/td');

        perl -e 'print qq(Just another Perl Hacker\n)' # where's the irony switch?
Re: How to output the words that you want that came thru an html file?
by NetWallah (Canon) on May 04, 2012 at 05:19 UTC
    grep returns a list. In this case, it is a list of ONE element. When you assign a list to a scalar, you get the number of elements.

    try:

    my ( $introduction ) = grep /Game Introduction - Marvel: Avengers Alli +ance/, @content; # Parens added in order to provide LIST context, and removed where not + necessary

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Oh I see... I tried and it printed the whole html code. It's the same if I printed the @content..