Harry Putnam <[EMAIL PROTECTED]> wrote:
: 
: "Charles K. Clarkson" <[EMAIL PROTECTED]> writes:
: 
: > : I wanted a way to ensure that each reg has hit at
: > : least once.  Otherwise we don't print.  So I used a
: > : formulation like this (Not posted previously for
: > : clarity):
: > : 
: > :          if ($data{$hdregs[$ii]}++ == 0) {
: > :            ## it will only be 0 once
: > :            $hdelem_hit_cnt++; 
: > :          }
: > : Then before printing we compare $hdelem_hit_cnt to
: > : ($#hdregs + 1):
: > : 
: > :  sub test_hdr_good {
: > :     if ($hdelem_hit_cnt == ($#hdregs + 1)) {
: > :       $test_hdr_good = "TRUE";
: > :       $hdelem_hit_cnt = 0;
: >
: >     Generally, global variables should raise a giant,
: > blinking, annoying sign telling us we an are no
: > longer in Kansas.
: 
: I didn't post it but in fact I have a `my' declaration
: like this at the beginning of my `sub wanted {'
: 
: my($line,@hdhits,$hdelem_hit_cnt,%data);
: 
: Another one at the beginning of the script that trys
: to catch everthing that didn't need to be local to a
: loop of some kind.

    As programs get larger, many perl programmers find
declaring variables at the top of a code structure a
poor idea. In perl it is generally better to declare a
variable at or very near it's first invocation.

    I realize you wanted to keep your message brief and
I applaud your efforts, but (IMO) you left out relevant
parts of test_hdr_good(). It looks like $test_hdr_good
and $hdelem_hit_cnt are globals declared /outside/ the
sub. If that is not the case you should have included
that part of the sub. If it is the case, (unless
$test_hdr_good, @hdregs, and $hdelem_hit_cnt are local
 to an unnamed block containing related subs, which
doesn't seem the case) you are using global variables.


 
: > : They should be the same if all regs have hit at least
: > : once.  If not the same... we don't print.
: >
: >     Actually, they should be the same if all regs were
: > hit /only/ once.
: 
: No, you'd have to try it to see that is not true.
: 
: That was the beauty of it to me.  It would only increment
: if a UNIQ hit happened but not if other repeated hits
: happened. So a repeat hit would not get mistaken for a
: UNIQUE hit and throw off the count.

    Later in the test_hdr_good() sub we this comparison:

if ($hdelem_hit_cnt == ($#hdregs + 1)) {


    (Assuming $hdelem_hit_cnt and @hdregs are global):

    To be true $hdelem_hit_cnt has to have the same value
as the number of elements in @hdregs. Since $hdelem_hit_cnt
won't increment if it finds a duplicate in @hdregs all of
@hdregs elements must be unique unless something unseen
is working on %data, $hdelem_hit_cnt, or @hdregs.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to