Hey Nick,
I had only a few minutes to play with this when it first came up, but I couldn't accomplish what I wanted with the time I had. What I did have was very similar. That said, you've hard-coded your array when doing the grep(), which probably isn't realistic in production, and it's a bit outside of what OP was after. To do it out of order and without hardcoding, an extra sort needs to be put in place. It could be done inline, but this makes it a bit more clear I think:
use strict; use warnings; use feature 'say'; my @x = ( -2, 3, 2, 10, 15, 8 ); my %y = map { $_ => 1 } @x; @x = sort {$a <=> $b} @x; say "missing: $_" for grep { ! exists $y{$_} } $x[0] .. $x[-1];
In reply to Re^2: Better way of writing "find the missing number(s)"
by stevieb
in thread Better way of writing "find the missing number(s)"
by pritesh_ugrankar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |