jdavis wrote:
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.

thanks,

#! /usr/bin/perl -w


use LWP::Simple;

$item_string = '\<\!\-\- \#\#\# Display Item\'s Name and make a
hyperlink \#\#\# \-\-\>';
$price_string = '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);
                 print "ITEM = $clean_item\n";
                
                 do{
                   $_ = 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);
                 print "PRICE = $clean_price\n";
                 $price_items{$clean_item} = $clean_price;
        }
}

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


"Comes out all weird" is a very poor bug description. I am assuming you are running this at a terminal window?? Rather than apologizing for the message wrapping, you should instead apologize for not using strict. And the above code is the reason for my earlier ramblings about the $_ and why it is nice to have named variables to yell at, especially when performing functions like pop. Now having ranted enough, let's look at your current problem...

It appears that there is an extra control character on the end of the
'clean_item' that is screwing up the output.  Initially I knew something
was up because I used Data::Dumper to check your structure which was
fine, and its' output was wacky, so I had it redirected to a text file
which opened in Vim shows that your structure is correct but also
displays the Ctrl+M character.  Where he is coming from I have no idea
and honestly don't care much, but you can successfully lop him off with:

$clean_item =~ s/\015//g;

In just the right spot...

http://danconia.org

p.s. I do assume you have permission to screen scrape the contents.....
This can be seen as a denial of service attack, especially with a couple
hundred beginner's all firing up lwp::simple to get the html to debug
the problem... over... and... over... and .... over...



--
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