in reply to Re: Re: Re: Reducing repetitive code
in thread Reducing repetitive code

Hmm, all of those could be solved by doing proper input validation. Also, for your second point, just indexing the array doesn't grow it, at least not on my version of perl:

$ perl -MDevel::Size -e '@a = (0 .. 11); print Devel::Size::total_size +(\@a)' 296 $ perl -MDevel::Size -e '@a = (0 .. 11); $i = $a[56788]; print Devel:: +Size::total_size(\@a)' 296 # Just to make sure perl doesn't optimize away the assignment $ perl -MO=Deparse -MDevel::Size -e '@a = (0 .. 11); $i = $a[56788]; p +rint Devel::Size::total_size(\@a)' -e syntax OK use Devel::Size; @a = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); $i = $a[56788]; print Devel::Size::total_size(\@a); $ perl -v This is perl, v5.8.0 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2002, Larry Wall Binary build 806 provided by ActiveState Corp. http://www.ActiveState. +com Built 00:45:44 Mar 31 2003 # snip the rest

What would grow the array is autovivification, such as if you said $i = $a[57874]{key};.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re^4: Reducing repetitive code
by duff (Parson) on Jan 06, 2004 at 20:37 UTC

    BTW, you'll note that you don't see me arguing the merits of the array solution with dragonchild even though I'm the one who suggested it. Because, of course, I agree with him 100%. Be as paranoid as you think necessary because good programmers are always paranoid about their data :-)

    anyway ... cheers,
Re: Re^4: Reducing repetitive code
by dragonchild (Archbishop) on Jan 06, 2004 at 20:17 UTC
    Hmm, all of those could be solved by doing proper input validation.

    That's what I am doing with the hash method.

    just indexing the array doesn't grow it

    You're right ... so long as you're using a newer Perl. Try it on 5.005_3, which is still a very common Perl version in use. (Many companies don't want the risk of recompiling all their modules for 5.6.x or 5.8.x, so they stick with 5.00x_x.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.