[Gllug] perl script

Simon Stewart sms at lateral.net
Thu Aug 22 09:21:30 UTC 2002


On Thu, Aug 22, 2002 at 09:53:54AM +0100, Tethys wrote:
> 
> >opendir (DIR, '.');
> >foreach ( grep {/\.jpeg$/} readdir DIR ) {
> >    /^(.)(.)(.)(.)(.)(.*)$/;
> >    rename ($_, "$4$5$3$1$2$6");
> >}
> 
> Proof positive once again that perl should be avoided wherever possible!
> Compare the readability of that with:
> 
> 	#!/bin/sh
> 
> 	for i in *.jpeg
> 	do
> 		n1=`echo "$i" | cut -c1-2`
> 		n2=`echo "$i" | cut -c3`
> 		n3=`echo "$i" | cut -c4-5`
> 		n4=`echo "$i" | cut -c6-`
> 
> 		newname="${n3}${n2}${n1}${n4}"
> 
> 		mv "$i" "$newname"
> 	done

Anyone who's used UNIX for longer than a week will have run into
regular expressions. In this particular case, I really do think that
the regex in the first script is perfectly readable (clunky, perhaps)
whereas assigning variables using the "cut" command is the more
obfuscated root. Why? Because the first script clearly says "match the
default input, making a note of the following 6 locations, then rename
it to this" which the second one doesn't.

Also, on a pedantic note, it would be possible to alter the first
script to cope with variable length digits more easily than the second
(I'm thinking "s/^(\d+?)_(\d+?)(.*)/$2_$1$3/" but I've not tested
this) Thinking about it, the minimum munch isn't necessary, but you
might get some more speed out of the script that way.

If this makes no sense, it could be because I've not had my early
morning coffee yet.

Cheers,

Simon

-- 
Clothes make the man. Naked people have little or no influence on society.
                -- Mark Twain

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list