### Re: Flag variables (to represent state)

by grinder (Bishop)
 on Mar 03, 2003 at 16:13 UTC ( #240063=note: print w/replies, xml ) Need Help??

Interesting question, I've never given it much consideration and I'm guilty of using somthing like it. Consider a snippet I wrote recently: Ordering hash replacements to avoid clobbering things (update chaining). At one point I check whether two arrays are equal.

In other words, @one = qw/a b c/; @two = qw/a b c/; die if @one == @two; (where == in this instance is a deep equality: each element is compared in turn (and thus will die with these inputs)). As soon as two elements differ, I know the arrays can't be the same, so I can bail out early.

The code looks like:

```    my \$is_loop = 1;
for( my \$n = 0; \$n < scalar @loop_keys; ++\$n ) {
if( \$loop_keys[\$n] ne \$loop_vals[\$n] ) {
\$is_loop = 0;
last;
}
}
die if \$is_loop;

I suppose there's a more compact way if saying that, but I pondered the question for about 1.38 seconds before banging out the above code. I assume perl 6's hyper operator is going to be of great help to me in this respect.

Note that I'm actually doing the opposite: seeing whether the flag still has the same value, not whether it has changed.

I often resort to this type of algorithm. It's like setting up a hypothesis at the beginning of the loop and seeing whether it can be disproved. The flag variables always seem to be named \$found or \$is_foo. In this particular case, it's easy to see whether the two are different. As soon as one pair fails to line up, you know they can't be equal, hence you can leave early (in the particular domain this code comes from it's usually the first iteration).

update: a few wordos and typos fixed

Another update: As IlyaM points out, there are better ways of doing the above example. That's the problem with contrived examples. But the idea still stands that you start in a certain condition, do a whole lot of stuff and then see if you remained in the initial condition.

You might move out of the condition quickly, or not at all. It's more the idea of short-circuiting a possibly lengthy examination... in the worst case you will have to examine every single element anyway, but if you can bail out early, so much the better.

print@_{sort keys %_},\$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r\$s-t%t#u'

Replies are listed 'Best First'.
Re: Re: Flag variables
by IlyaM (Parson) on Mar 07, 2003 at 11:37 UTC
Why not just?
```
for( my \$n = 0; \$n < scalar @loop_keys; ++\$n ) {
if( \$loop_keys[\$n] ne \$loop_vals[\$n] ) {
die;
}
}

or more Perl-ish
```
for my \$n ( 0 .. @loop_keys - 1 ) {
if( \$loop_keys[\$n] ne \$loop_vals[\$n] ) {
die;
}
}

--
Ilya Martynov, ilya@iponweb.net
CTO IPonWEB (UK) Ltd
Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
Personal website - http://martynov.org

Create A New User
Node Status?
node history
Node Type: note [id://240063]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2018-11-17 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My code is most likely broken because:

Results (205 votes). Check out past polls.

Notices?