Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While loop

Status
Not open for further replies.

titosat

Technical User
Jan 17, 2007
8
US
Hi I have a problem with the "while" loop it keeps printing the same line but when i go to the page source code there are a cople of line with the same syntax pls someone tell my what is the prob here thz
Here is the code
Code:
use strict;
use warnings;
use HTTP::Cookies;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use HTTP::Request::Common qw(GET);


my $ua = new LWP::UserAgent;
my $cookie_jar = HTTP::Cookies->new(file => 'pansatdirectcookies.txt', autosave => 1, ignore_discard => 1);
$ua->cookie_jar($cookie_jar);

my $url = '[URL unfurl="true"]http://www.pansatdirect.net/beta2/catalog/index.php?cPath=26';[/URL]

my $req = GET $url;
my $res = $ua->request($req); 

if ($res->is_error()) {
     printf " %s\n", $res->status_line;
 } else {
my $content = $res->as_string;


#My prob. is here
while ($content =~ /<td align="center" class="productListing-data">(.+)<a href="(.+)"><img src="(.+)" border="0" alt="(.+)- <font color=(.+)>(.+)<(.+)title="(.+)- <font color=(.+)>(.+)<(.+)" width="160" height="128"><\/a>(.+)<\/td>/m) {
  print "$2\n";
  print "$4\n";
  print "$6\n";
}
}
}
 
Hi , didnt do much.. just changed that while loop to a foreach loop. Just take a look n see if this serves ur purpose..
Code:
use strict;
use warnings;
use HTTP::Cookies;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use HTTP::Request::Common qw(GET);


my $ua = new LWP::UserAgent;
my $cookie_jar = HTTP::Cookies->new(file => 'pansatdirectcookies.txt', autosave => 1, ignore_discard => 1);
$ua->cookie_jar($cookie_jar);

my $url = '[URL unfurl="true"]http://www.pansatdirect.net/beta2/catalog/index.php?cPath=26';[/URL]

my $req = GET $url;
my $res = $ua->request($req); 

if ($res->is_error()) {
     printf " %s\n", $res->status_line;
 }
else {
    my $content = $res->as_string;
    my @lines=split /\n/,$content;

    #See if your Problem is resolved here
    foreach my $line (@lines){
        if($line =~ /<td align="center" class="productListing-data">(.+)<a href="(.+)"><img src="(.+)" border="0" alt="(.+)- <font color=(.+)>(.+)<(.+)title="(.+)- <font color=(.+)>(.+)<(.+)" width="160" height="128"><\/a>(.+)<\/td>/m)
        {
            print "\$2\::$2\n";
            print "\$4\::$4\n";
            print "\$6\::$6\n\n";

        }
    }
}
 
try putting the 'g' option on the end of the regexp in the conditional part of the while loop:

Code:
while($conent =~ /your long regexp here/mg){
   your code here
}





- Kevin, perl coder unexceptional! [wiggle]
 
thz for the fast response I use the "devashiya" code and it work great. I have n othere "?" how do i set a permanent var. that will only be change the next time the I run the script. thz in advance
 
You want a variable to still exist after your script ends and you start it again? Try saving something to a file.

Code:
#!/usr/bin/perl

our $saved; # saved var

if (-f "saved.txt") {
   # something was saved from the last run
   open (READ, "saved.txt");
   $saved = <READ>;
   close (READ);
}

#...run your program...

# save the variable
open (WRITE, ">saved.txt");
print WRITE $saved;
close (WRITE);

If you want to deal with multiple variables at once, try putting them in hash form and use Data::Dumper.

Code:
use Data::Dumper;

our $structure = {
   cat => 'meow',
   dog => 'bark',
   horse => 'neigh',
};

# save it
open (WRITE, ">saved.txt");
print WRITE Dumper($structure);
close (WRITE);

# load it
our $structure = do "saved.txt";

-------------
Cuvou.com | The NEW Kirsle.net
 
or use the Storable module.

- Kevin, perl coder unexceptional! [wiggle]
 
thz guys I will try to save it to a text file. Like Kirsle say.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top