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

Re: [MacPerl] testing



At 9:14 AM +0000 12/15/99, Bart Lateur wrote:
>On Wed, 15 Dec 1999 00:12:33 -0800, Bruce Van Allen wrote:
>
>>[ NOTE: Too bad we can't do this:
>>    keys %test = ($a, $b, $c, $d);  ]
>
>You can do this:
>
>	@test{$a, $b, $c, $d} = ();

Wow! Thanks, Bart. I missed that one. Too focused on the keys 
function. After testing, I see it's as you have it above, not:
     %test{ $a, $b, $c} = ();  #wrong to use '%'

And further, we can do this:

    @items = qw(a b c d e f 5);
    @test{@items} = ();

Wow! again. For some reason I had stuck in my mind that puffing up a 
hash from a list of keys required a loop structure...

>but that will make the value of all those keys undef (false). You must
>test presence of a key in the hash with exist().

Understood. That's what I was suggesting to the original poster, a la:

     exists($test{$value})

Incidentally, I agree with other posters' suggestions re using 
"switches" for easily seeing what's happening in your code. Another 
rule of thumb that sometimes affects my choices for control flow is 
avoiding large conditional structures in loops, to avoid cycling 
through a multiplicity of tests. In such cases, populating a hash 
outside the loop, and testing your value inside the loop by the 
existence of a hash element, is more efficient. As it says at the end 
of the Blocks and Switches chapter of the online docs 
(<http://www.perl.com/pub/doc/manual/html/pod/perlsyn.html>),
 
    "You might also consider writing a hash instead of synthesizing a 
switch statement."

I tend to write first in switches to rough out the flow of control, 
and then migrate as appropriate to hashes later when I'm optimizing 
my code.

Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:

>If you don't know the number of variables, your proposed syntax isn't
>practical either.

[This was referring to the use of a hash]

Well, if the hash is populated within my script, by assigning a set 
of variables to be the hash's keys, then I don't have to know those 
variables or their values ahead of time. The example I gave,

    foreach (qw(a b c d)) {$test{$_}++}

populates the hash using this list of literals (qw(a b c d)), but 
could just as easily have used

    foreach (@vars) {$test{$_}++}

where @vars is a list created within my script, by _any_ means 
including use of references. And of course this would work with

    @test{@items} = ();

So, Anno, unless we're talking about different things :-), I'll still 
say that the syntax I've shown _does_ work even if I don't know the 
number of variables in advance.

For the original poster, Nicholas, a lot of the above comes down to 
the algorithm; when does your script know what's in the variables, 
and when does it need to test a value against those variables?

HTH

1;

- Bruce

_Bruce_Van_Allen___bva@cruzio.com__831_429_1688_V_
_PO_Box_839__Santa_Cruz_CA__95061__831_426_2474_W_

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