"Charles K. Clarkson" <[EMAIL PROTECTED]> writes:

[...]

>     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 appreciate you letting me borrow some of your experience.

>     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.

Yes, I see now.

I think I may not have understood the meaning of `global'.  I
understood it to mean these variables could theoretically be exported
as in the case of `do "./this_script"; from another script.

I was under the impression that a `my($var);' at the beginning of
`this_script'  would
prevent that... but without testing this, I'm guessing I may have it
wrong.

> : > : 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.

I've managed to confuse this I'm afraid.  Your original statement on
this:     
   Actually, they should be the same if all regs were
  hit /only/ once.
Is not true.  They can hit more than once and should still be true or
equal.  

I confused things a bit by saying @hdregs wasn't intended to be
uniq.  I really meant the hits array was not intended to be
unique. And had read that into your comments.  That is, I thought you
were speaking of the hits array, eventhough you clearly stated
@hdregs. 

But in any case it appears to me that the technique does just what I
needed.

Can you think of a more compact or reliable way to test for all regex
having hit at least once?

This snippet of (possibly awkward poorly written) code seems to bear
out what I'm seeing. The regexs' hit more than once but the counter
keeps accurate track of whether all regex have hit or not.  So again,
your statement that `they should be the same if all regs were hit ONLY
once".  Is not true.  That was really all I had an objection to.

cat uniq_cnt.pl
  #!/usr/local/bin/perl -w
  
  my %data;
  my @script_array = qw( one two three );
  
  @myarr = <>;
  
  for(@myarr){
     chomp;
     $line = $_;
     for(@script_array){
       $reg = qr/$_/;
       if($line =~ /$reg/){
         push @matches, $line;
         ## records the fact that this reg has hit at least once
         if($data{$reg}++ == 0){
            $uniq_hits++;
         }
       }
     }
  }
  for (@matches){
       print "$_\n";
  }
  print "\$#sarray + 1=<" .  ($#script_array + 1) . "> \$uniq_hits=<$uniq_hits>\n";

$ echo -e "one\ntwo\nthree\ntwo\nthree" |./uniq_cnt.pl
one
two
three
two
three
$#sarray + 1=<3> $uniq_hits=<3>

-- 
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