[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl-AnyPerl] optimizing a beginners script



hi,

using the copy module i´ve written a (beginners)script, which will look in a
txt.file to detecte images (gifs or jpgs) which at the same time also occur
in a directory named "pics". when found in the txt.file it will make the
relevant directory-name and copy the relevant image into that directory just
created. the whole stuff goes into a direcory called "copied".

it does seem to work but i would greatly appriciate if someone could come
with a few pointers as for how to optimize the script - it is very slow when
tested on large amounts of images (2-3000)(the txt file is also large (3.000
lines)), so thats why i´ve put a timer in the script.

thanks in advance
allan

#!/pack/collect/bin/perl

$start = (times)[0];

opendir(COPYDIR, "pic") or  die "cant open pic";
@pics = readdir(COPYDIR);
closedir(COPYDIR);

open(FRONTHTML, "dump.txt")or die "cant open dump.txt";
@search = <FRONTHTML>;


for ($i=0; $i<=$#pics; $i++)
	{
	$dollar = @pics[$i];
	if ($dollar =~ /[a-z0-9_]\.(gif|jpg)/ig)
		{
		foreach $line (@search)
			{	
			 if ($line =~
/(=\"?\/?.*?(\b[a-z0-9_]*?\b))?\/?($dollar)\"?/ig)
			 	{
				$directory = $2;
				
				opendir(COPYMASTER, ".") or die "unable";
				@nomast = readdir(COPYMASTER);
				closedir(COPYMASTER);
				mkdir("copied/$directory", 0666);	

				use File::Copy;
				
	
copy("pic/$dollar","copied/$directory/$dollar");
				copy("Copy.pm",\*STDOUT);'
				
				use POSIX;
				use File::Copy cp;
				
				$n=FileHandle->new("/dev/null","r");
				cp($n,"x");'
				}
			#else
			#	{
			#	unlink "pic/$dollar";
			#	}	
			}
		}
	}

$end = (times)[0];
$secs = $end - $start;
	
open(FOUR, ">time.txt") ;
print FOUR $secs;
close(FOUR);

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org