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

SV: [MacPerl-AnyPerl] optimizing a beginners script



dear bart

thank you very much for your sugegstions - some of which were so obvoius
when you pointed them out (like using -e) some of which i had no idea.

i had to modify the script slightly but it does work and it is incredibly
fast too
i guess thats one of the beautiful things about perl having a task fixed by
a dozen or so lines of code ...

thanks

allan

final code:

#!/pack/collect/bin/perl -w

use File::Copy;
  @ARGV = "dump.txt";
  while(<>) 
  	{
	chomp;
	s/.*?=// or next;
	s/^"// and s/"$//;
	($dir, $file) = /(\w+)\/([^\/]+\.(gif|jpg|jpeg))/i or next;
	-e "pic/$file" or next;
	mkdir ("copied/$dir", 0777);
	copy ("pic/$file", "copied/$dir/$file");
	}




org message:
_____________
On Tue, 25 Jul 2000 13:18:08 +0200, Allan Juul wrote:

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

That looks like a pretty simple requirement. I can't see why you need
such a huge script. First, some small remarks; after which I'll propose
my own idea.

>	$dollar = @pics[$i];

You're not running the script using -w, are you? See the FAQ,
perlfaq1.pod:

  What is the difference between $array[1] and @array[1]?


>	if ($dollar =~ /[a-z0-9_]\.(gif|jpg)/ig)

"[a-z0-9_]" under //i is the same as "\w". So you're looking for files
with a name ending in a word character, followed by typical image file
extension.

But, why is it not anchored? Not that it horst that much...

>		{
>		foreach $line (@search)
>			{	
>			 if ($line =~
>/(=\"?\/?.*?(\b[a-z0-9_]*?\b))?\/?($dollar)\"?/ig)

You've lost me here. You need a file specification following an "="
sign, optionally between quotes, with a directory specification for
which you only need the final \w+ part as a directory?

>			 	{
>				$directory = $2;
>				
>				opendir(COPYMASTER, ".") or die "unable";
>				@nomast = readdir(COPYMASTER);
>				closedir(COPYMASTER);

Now this is REALLY confusing. You're reading the contents of a directory
for each file, and you're not using the reasul anywhere else.

>				mkdir("copied/$directory", 0666);	

Hmmm... can you even create a new file in that directory? I think you
need X permissions for that.

>
>				use File::Copy;
>				
>	
>copy("pic/$dollar","copied/$directory/$dollar");

alright...

>				copy("Copy.pm",\*STDOUT);'

Heh?

>				
>				use POSIX;
>				use File::Copy cp;
>				
>				$n=FileHandle->new("/dev/null","r");
>				cp($n,"x");'

I don't understand this. It seems like just a waste of time.

>				}
>			}
>		}
>	}

So here's my proposal. You read from the file. For each line, you try to
extract the directory name and file name from the path. If it fails, or
if it's not an image filename, skip to the next line. You see if the
file exists. If it does, copy the file to it's new location.

  use File::Copy;
  @ARGV = "dump.txt";
  while(<>) {
      chomp;
      s/.*?=// or next;
      s/^"// and s/"$//;
      my($dir, $file) = /(\w+)\/([^\/]+\.(?:gif|jpg|jpeg))$/i or next;
      -e "pic/$file" or next;
      mkdir $dir, 0777;
      copy "pic/$file", "copied/$dir/$file"
        or warn "Can't copy file $file: $!\n";
  }

That's it. Untested, though.

Could anything be following the filename in dump.txt, apart from a quote
character?

-- 
	Bart.

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






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