hi,
from time to time i have been wondering if it would not be nice i i could do: foo[3,7,4,2]; and have that return an array ({ foo[3], foo[7], foo[4], foo[2] })
today i thought about this problem once more and i just had the revelation that i could do: foo[ ({ 3,7,4,2 })[*] ]; to produce the desired result.
to some of you this may be an old hat, but to me it was a pleasant surprize. pike never ceases to amaze me! thanks guys!
greetings, martin.
It was one of the recipies when we did some "Pike cookbook" work.
Title: Multi index Contributor: Martin Nilsson Problem: You have a set of indices into an array and want to create a new array built from the values from the first array.
Solution:
array multiindex(array arr, array ind) { return arr[ind[*]]; }
Discussion:
This is a use for the automap operator that isn't obvious to come up with. To help you make sense of the expression above the expression could be verbalized as "indexing arr with, in turn, all the indices in ind and putting all the result values in an array".
Note that in most cases when this problem appears it is better avoided all together than solved. One typical example is that you have one array with a set of values and several arrays with different sortings, or different (sorted) subsets. In Pike there is no waste of memory to store the actual values in all arrays instead of just pointers into a value array. You'll actually save some by not having a value array.
pike-devel@lists.lysator.liu.se