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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with search code

Status
Not open for further replies.

calypso13

Technical User
Nov 5, 2004
21
US
I am fairly new to PERL. I have this code that searches a directory called /webpics which contains like several other image folders that contain folders with starting and ending with digits. like /webpics/originals/010/012345.jpg.
I have a module with a package name "product" that contains subroutines. However, the my $path_to_images is not working properly for multiple folders, liike when i search for an image with a sku number, it only searches the first directory /webpics/originals and ignores the rest. How do I clearly and recursively search the /webpics directory without having to write each directory as $path_to_images = somedirectory; over and over again for each one.
Also, all sku numbers are 6digits so I have ($sku =~ /^\d{3}/) but some of the images have eg. 012345.jpg and 012345-all and 012345-BIG, I have tried some patterns but just hasnt worked right. how do I make it search both digits and digits with words ignoring lower or upper cases?
I know its a lot of questions but I have been at this for weeks and seriously need some help. ANY HELP..
thanks! :)

#!/usr/bin/perl -w
use CGI;
use strict;
use product qw:)productInfo);
use Cwd;
$|++;

my $q = new CGI;
my $sku = $q->param('sku');
my $path_to_images = "/webpics/originals/";
my $path_to_images = "/webpics/itemthumb/";
my $path_to_images = "/webpics/itemmed/";


print $q->header();

#check for sku, description associated with it
#then print the image
if ($sku){

my $firstDigits;

if ($sku =~ /^\d{3}/) {$firstDigits = $&}

my %product = getProductInfo($sku);
$path_to_images .= $firstDigits . "/" . $sku . ".jpg";
if (-e $path_to_images && values %product)
{

print <<HTML;
<center>
<table width="200" border="1">
<tr align="center" valign="middle">
<td>
<img src="/webpics/originals/$sku.jpg">
</td>
<td> <img src="/webpics/originals/$firstDigits/$sku.jpg">
</td>
<td><img src="/webpics/itemthumb/$firstDigits/$sku.jpg"></td>
</tr>
</table>
<table width="400" height="100">
<tr>
<td align="left">
Sku Number: <b>$sku</b><br>
Item Number: <b>$product{'item_number'}</b><br>
Minimum Sell: <b>$product{'min_amount'}</b><br>
Description: <b>$product{'description'}</b><br>
Retail: <b>\$$product{'retail'}</b><br>
</td>
</tr>
</table>
</center>
HTML
}
else{

print <<HTML;
<center>
<font color="red">Sorry, No Products Avaliable with that Sku Number.</font><br>

</center>

HTML
}
}
print scalar localtime, $q->end_html;
 
Hi,

That's two problems really:

1 - Searching recursively down a directory tree

2 - And pattern matching on the file-names you find.

Ok - 1 - Recursive searching.

The File::Find module is for you, does exactly what you need.

Have a look at that, get directory recursion going first and then look at 2.

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top