Jonathan Scott Duff wrote:
I think that if there were a slice-based form of grep, it would most likely look like you are indexing by a subroutine (or method) reference that takes no arguments other than an element of the array. Something like:On Wed, Nov 06, 2002 at 12:54:12PM -0500, Mark J. Reed wrote:On 2002-11-06 at 11:43:20, Jonathan Scott Duff wrote:I think what you mean here is justWill there be some shorter-hand way to say these? @a = @grades[grep $_ >= 90, @grades]; @b = @grades[grep 80 <= $_ < 90, @grades]; @c = @grades[grep 70 <= $_ < 80, @grades];
@a = grep $_ >= 90, @grades;
etc. grep returns the actual elements, not their indices, so it doesn't
make sense to use them as a slice.Er, yeah. I must be subcaffienated right now. :-( What I was trying to get at was the ability to slice based on the values instead of the indices. But maybe it's just the python programming that I've been doing that makes me think it's useful. -Scott
@a = @grades[{$^x > 90}];
or
@a = @grades[-> x { $x > 90}];
or
my StudentGrade @grades;
@a = @grades[.isPassing];
I could see that as being useful.
Of potentially more use would be:
%grades = { Tom => 85, Mary => 95 };
%a_students = %grades{-> $name, $grade { $grade > 90 } };
print keys %a_students; # 'Mary'