[Tutor] Perl refugee

dman dman@dman.ddts.net
Fri, 26 Apr 2002 21:38:20 -0500


--opJtzjQTFsWo+cga
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Apr 26, 2002 at 07:59:32AM -0700, Rich Pinder wrote:
| Ok... i'm a zope user (not a good one), and long time perl user.
| Each time i get a little 'utility' type project, I think "A ha..  I'll
| use Python this time" - then the time gets low, i never get around to
| it, and say  "heck, i'll just do it in Perl".....
|=20
| So this morning I said no - wanna get the first one under my belt !

Good!  You won't regret it!  (zope is really cool too, isn't it?)

| realized the machine i'm using doesnt have Python - so loading it now.
|=20
| .... here's my goal.....
|=20
| I need to copy 80,000 files from one directory and put them in one of 20
| other directories.  I'm into chaper 12&13 in the pyton book, and am sure
| I can get the looping structure over the directory listing....
|=20
| But to progam the logic to decide where the files go, I need substring
| parsing of the file names (think thats easy)

Yeah python is quite good at string manipulation.  Unless you write
some really wacky code you'll even be able to comprehend it in a few
weeks!

| PLUS something like a CASE statement.  I don't see the CASE
| statement anywhere.

A switch/case statement is just a hangover from C.  It is faster
because it is simply a computed jump, but as a result is restricted to
int and char.

| So will i have to use a bunch of cludgy if/then statements, or is there
| something more elequent...
|=20
|   if subval > "0001"  and subval < "0010" :

You really don't want that in a switch either!  You'd have to write
out each and every number and have them fall through.  Better to use
an if-else ladder.

|      <filecopyingsteps>
|  elif subval > "0009"  and subval < "0020" :
|      <filecopyingsteps>
| ........

Change
    <filecopyingsteps>
to be a function call instead.  Write your function generally enough
so that you can pass some simple arguments to it from the if-else
ladder and have it correctly handle all (or at least many) of the
operations you need to do.

Do something like :

if "0001" < subval < "0010" :
    # you didn't actually mention the parameters for moving the files,
    # so I'm just wildy guessing here
    copyfile( subval , "subdir1" )
elif "0009"  < subval < "0020" :
    copyfile( subval , "subdir2" )


It really isn't that bad, now is it?

IME switch statements are rarely used (even in C/C++/Java) anyways,
and in the situations where it might be useful a dictionary lookup can
be much more elegant.

-D

--=20

In the way of righteousness there is life;
along that path is immortality.
        Proverbs 12:28
=20
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg


--opJtzjQTFsWo+cga
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjzKDxwACgkQO8l8XBKTpRQsCQCgyIm1H6lOOh82VkzqEWcM8sVT
FEEAnjqEGImrekevh22jPxvqM9O5bb1n
=DqK6
-----END PGP SIGNATURE-----

--opJtzjQTFsWo+cga--