[Gllug] Perl scripting challenge

Richard Jones rich at annexia.org
Wed Oct 4 15:56:58 UTC 2006


On Wed, Oct 04, 2006 at 04:47:01PM +0100, - Tethys wrote:
> On 10/4/06, Simon Morris <simon.morris at cmtww.com> wrote:
> 
> >Problem there is I don't know what the duplicated filenames are...
> >Apparently on this filesystem (I'm about to inherit a problem unless I
> >can deflect it away :-) ) they had a history of creating filenames that
> >are identical apart from case.
> 
> No problem:
> 
> #!/bin/sh
> 
> find "${1:-.}" -type d -print | while read dir
> do
>    preserve_case=$(ls -a1 "$dir" | wc -l)
>    ignore_case=$(ls -a1 "$dir" | sort -f | uniq -i | wc -l)
>    if [ "$preserve_case" != "$ignore_case" ]
>    then
>        echo "Directory has duplicate filenames: $dir"
>    fi
> done

But the problem is that he wants to know what the duplicated filenames
are, right?  Knowing that there are duplicate filenames is obviously
much simpler.

FWIW, here's the deliberately obfuscated OCaml version, not because
it's shorter (it isn't) nor because it's a good example of programming
(it's not), but because people might enjoy working out how the heck it
works.

----------------------------------------------------------------------
#!/usr/bin/ocamlrun ocaml

module S = String
module L = List
let t = Array.to_list;;

L.iter (
  fun path ->
    let c a b = compare (S.lowercase a) (S.lowercase b) in
    let pr acc acc' = if acc <> [] then print_endline (S.concat ", " acc') in
    let rec loop acc = function
      | [] -> pr acc acc
      | [x] -> pr acc (x :: acc)
      | x :: y :: xs when c x y = 0 -> loop (x :: acc) (y :: xs)
      | x :: y :: xs -> pr acc (x :: acc); loop [] (y :: xs)
    in
    loop [] (L.sort c (t (Sys.readdir path)))
) (L.tl (t Sys.argv))
----------------------------------------------------------------------

Spoiler follows below fold.



Think of the Unix 'uniq' utility.

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com - NEW!
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list