searching a sub-string in a string

Im trying to download a file from a website.

But i want to download only if the file version is latest than the same file I have.

The file name is CTP-LATEST-5.3.0.37.iso

I need to check the string "37" in the file name.

So if the number 37 is greater than the version Im already having then only I download.

Once I download the file I want to store the string "37" is a file so that I can read it next time when I run the perl code to check for version

Here is my code Im trying with

#!/usr/bin/perl 
use LWP::Simple; 
 #my $version =37;//this whole line is commented 
Open file version.txt and get the version number(say 37)and store it in $version     
 
 
$pageURL="http://www.google.comisos/psfsS5.3/LATIMGODCVP/";  
 
$simplePage=get($pageURL); 
 
Now $simplePage has the substring  "CTP-LATEST-5.3.0.(some_version_number).iso"  
 
I need to get that "some_version_number". HOW ? 
 
 
if(some_version_numner > $version) 
 
{ 
 
 
#-- fetch the zip and save it as perlhowto.zip 
my $status = getstore("http://www.google.com/isos/psfsS5.3/LATIMGODCVP/CVP-LATEST-5.3.0."$version".iso", "CTP-Latest5.3.0.$version.iso"); 
 
if ( is_success($status) ) 
{ 
 print "file downloaded correctly\n"; 
 
 Store the "some_version_number in file version.txt (for next time use) 
 
} 
else 
{ 
 print "error downloading file: $status\n"; 
 } 
 
} 

Please not that In the website there are other files with .iso extension , But I need the only the file with "CTP-LATEST-5.3.0." prefix

I hope everything is clear in code. The version.txt has only 1 string stored that is any number (say 37 or 45 or 89 etc )

Can someone help me on how to do the rest of the code ?

Hi,

Try this code,

my $oldVersion="38";##Version.txt File Version
my $simplePage ="CTP-LATEST-5.3.0.37.iso";
my $str = (split("CTP-LATEST-5.3.0.",$simplePage ))[1];
my $version = substr("$str",0,2);
if($version > $oldVersion )
{
   print "NewVersion\n";
   ####Proceed your further code
}
else
{
   print "old Version\n";
   ####Proceed your further code
}

Cheers,
Ranga:b:

Hi Ranga,
I did not understand some of your lines

1) my $simplePage ="CTP-LATEST-5.3.0.37.iso";

why are you hard coding this ?

I need to get this srting from webpage, get only "37" part and then compare it.

my $simplePage = get(webpage);

This is your code right??

$simplePage may contains data like SomeContent_CTP-LATEST-5.3.0.37.iso right?

Hi I tried your code this way

#!/usr/bin/perl
 
use LWP::Simple;
my  $oldversion =36;
 
$pageURL="http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/";   
my $simplePage=get($pageURL);  
#my $simplePage ="CTP-LATEST-5.3.0.37.iso";
my $str = (split("CTP-LATEST-5.3.0.",$simplePage ))[1];
my $version = substr("$str",0,2);
if($version =! $oldVersion )
{
print "$version";
 
##-- fetch the zip and save it as perlhowto.zip
my $status = getstore("http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/CTP-LATEST-5.3.0.$version.iso", "CVP-LATEST-5.3.0.$version.iso");
}
else
{
print("Currently new version\n");
}

but its printing $version as 1 , which is not the correct value. The correct value is 37 which its not able to print.

just print the $simplePage variable and get back me with that value and also send the possibilities.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /isos/preFCS5.3/LATESTGOODCVP</title>
 </head>
 <body>
<h1>Index of /isos/preFCS5.3/LATESTGOODCVP</h1>
<table><tr><th><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr><tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img src="/icons/back.gif" alt="[DIR]"></td><td><a href="/isos/preFCS5.3/">Parent Directory</a></td><td>�</td><td align="right">  - </td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="CVP-LATEST-5.3.0.37.iso">CVP-LATEST-5.3.0.37.iso</a></td><td align="right">19-Jul-2011 03:32  </td><td align="right">816M</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="ChangeLog-LATEST.2011-07-19-03h.30m.01s">ChangeLog-LATEST.2011-07-19-03h.30m.01s</a></td><td align="right">19-Jul-2011 03:32  </td><td align="right"> 16K</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="is.iso">is.iso</a></td><td align="right">19-Jul-2011 03:32  </td><td align="right">816M</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="md5SUM">md5SUM</a></td><td align="right">19-Jul-2011 03:32  </td><td align="right">111 </td></tr>
<tr><th colspan="5"><hr></th></tr>
</table>
<address>Apache/2.2.3 (Red Hat) Server at www.google.com Port 80</address>
</body></html>

here is the content of my page. You can see the filename marked in red. I want to extract only that file name and then from that filename I want to extract the number "37" or anyother number that is there is that place and then compare with already stored number.

37 may change to 38 or 45 or 98 in future days

Hi,

Try this one,

my $newPage = "$simplePage";
my $str = (split("href=\"CVP-LATEST-5.3.0.",$simplePage ))[1];
my $version = substr("$str",0,2);
print "Version: <$version>\n";

I think variable $simplePage doesn't have string data so need to convert to string. Try the above code.i assume href=\"CVP-LATEST-5.3.0. wont change in future.

Cheers,
Ranga:)

Ok, I see some wired behaviour,

#!/usr/bin/perl
 
use LWP::Simple;
my  $oldversion =36;
 
$pageURL="http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/";   
my $simplePage=get($pageURL);  
 
my $newPage = "$simplePage";
my $str = (split("href=\"CVP-LATEST-5.3.0.",$simplePage ))[1];   //here its again $simplePage or  $newPage ? 
my $version = substr("$str",0,2);
print $version;    // This displays 37  which is correct
 
if($version =! $oldVersion )
{
 
print $version;    // Here it displays 1  which is wrong and so the file dont download.
 
##-- fetch the zip and save it as perlhowto.zip
my $status = getstore("http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/CVP-LATEST-5.3.0.$version.iso", "CVP-LATEST-5.3.0.$version.iso");
}
else
{
print("Currently new version\n");
}
 

Should I convert the value to integer or something ?

Hi,

Make a note on bolded letters.
i think your variable $version overwrites somewhere.

Cheers,
Ranga:)

Ok, But where is it overwritting ? it can only be in

if($version =! $oldVersion )

so here its getting changed. But why ?

Also the IF statement is comparing with the new overwritten value.

Hi,

Look into your comparision Operators.

please make a change as,

if($version != $oldVersion )

Cheers,
Ranga:)

Yes I get it, but whether to use != or ne . Becuase $version is in the form of string and $oldversion is integer.
So eventhough both are equal the IF condition is getting TRUE and the file is downloading.

any idea how to convert to integers ? i tried int() and sprintf both did not work .

better use != for Numbers.

Cheers
Ranga:)

Hi thnank you so much.. I used != and it seem to be working fine. another problem was $oldversion and $oldVersion in IF loop.