Thank you very much, linuxer, this is indeed one line :-)
Still, is there no built-in function in perl which returns the indices of an array based on a boolean query?
I am moving to perl from matlab (don't want to get into fights about which programming languages are the best (or whether matlab can be considered as one in the first place ;-)), but I think matlab has an edge here, since the equivalent matlab code would be:
indexes BLOCK LIST
Evaluates BLOCK for each element in LIST (assigned to $_) and returns a list of the indices of those elements for which BLOCK returned a true value. This is just like grep only that it returns indices instead of values:
matlab is a special purpose tool tuned for handling arrays. Perl is much more a general purpose scripting language. It is unsurprising that there are elegant ways of performing tasks within matlab problem domain that Perl can't directly match. There are many things that Perl does nicely for which matlab has no equivalent. Ya pays ya money (a lot of money in the case of matlab if you're not in an educational institution) and takes ya choice.
I had a number of projects in the past where I used Perl to create input files for Matlab, mainly matrices, and then kicked off Matlab to do the processing of those, and then back in Perl created nice reports to present the results (LaTeX files). Using multiple tools (if available) can be very efficient, using each for what it is best for.