Jdavis wrote:
> 
> Hello,

Hello,

>  I have a script that gets items and corresponding price from a web
> site. all goes well until i put the item and price in a hash. When i try
> to print key and value from the hash it comes out all weired. Could some
> one take a look and see what im doing wrong.

Certainly.


> #! /usr/bin/perl -w

use strict;

> use LWP::Simple;
> 
> $item_string = '\<\!\-\- \#\#\# Display Item\'s Name and make a
> hyperlink \#\#\# \-\-\>';

You don't need to escape those characters, they are not special in
strings or regular expressions.

my $item_string = '<!-- ### Display Item\'s Name and make a hyperlink
### -->';

And since you are using this as a regular expression you can compile it
here instead of later.

my $item_string = qr/<!-- ### Display Item's Name and make a hyperlink
### -->/;


> $price_string = 'color="red"';

my $price_string = qr/color="red"/;


> $URL = 'http://shop.altenergystore.com/items.asp?Cc=SP100%2D';
> $content = get($URL);
> 
> # put each line of $content into a array
> @content = split(/\n/, $content);
> 
> @content_reversed = reverse @content;
> 
> while(@content_reversed){
>         $_ = pop(@content_reversed);
>         if(/$item_string/){
>                  $the_item = pop(@content_reversed);
>                  ($trash,$clean_item) = split(/\<b\>/, $the_item);

You don't need a $trash variable, you can use undef instead or use a
list slice on the split results.  You don't need to backslash '<' or
'>'.

        ( undef, $clean_item ) = split /<b>/, $the_item;

Or:

        $clean_item = (split /<b>/, $the_item)[1];


>                  print "ITEM = $clean_item\n";
> 
>                  do{
>                    $_ = pop(@content_reversed);
>                  }until(/$price_string/); ## this is the price line

You don't need the do{} block as you only have a single statement.

              $_ = pop @content_reversed until /$price_string/; ## this
is the price line


>       ($trash,$ruff_price) = split(/\<font color\="red"\>/,$_);
>       ($clean_price,$trash) = split(/\<\/font\>\<\/font\>/,$ruff_price);

    $ruff_price = (split /<font color="red">/)[1];
    $clean_price = (split /<\/font><\/font>/, $ruff_price)[0];


>                  print "PRICE = $clean_price\n";
>                  $price_items{$clean_item} = $clean_price;
>         }
> }
> 
> while(($k, $v) = each %price_items){
>         print "K = $k";
>         print "V = $v\n";
> }

Your problem is that $clean_item (your hash key) has a "\015" (CR)
character at the end.  When you print "K = $k" the carriage return moves
the cursor to the beginning of the line and "V = $v\n" prints over the
previous output.  The HTTP standard (RFC2616) defines CR LF as the
end-of-line marker and you are only removing the LF character.

A more simplified version of your code would be:

#! /usr/bin/perl -w
use strict;

use LWP::Simple;

my $URL = 'http://shop.altenergystore.com/items.asp?Cc=SP100%2D';
my $content = get( $URL );

my %price_items = $content =~ m{
    (?-x:<!-- ### Display Item's Name and make a hyperlink ### -->)
    .+?
    (?i:<b>)
    \s* (\S.*?\S) \s* $
    .+?
    (?i-x:<font color="red">)
    (\$\d[,.\d]+\d)
    (?i:</font>)
    }smxg;

while ( my ( $k, $v ) = each %price_items ) {
    print "K = $k  V = $v\n";
    }

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to