ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
delete_if_with_index or delete_at(array) in 1.8.6?
by Bil Kleb other posts by this author
Jul 2 2009 5:20AM messages near this date
ANN: Sequel 3.2.0 Released | Re: delete_if_with_index or delete_at(array) in 1.8.6?
Hi,

Given an array of integers, I need to delete all those
entries that are lower than the current array index.

I first thought of the non-existent,

  array.delete_if_with_index{ |e,i| e < i }

then,

  remove_indices = []
  array.each_with_index{ |e,i| remove_indices << i if e < i }
  array.delete_at( remove_indices )

or the same, but with a fictious inject_with_index,

  remove_indices = inject_with_index([]){|acc,e,i| acc << i if e < i }
  array.delete_at( remove_indices )

and finally the 1.9 possibility,

  array.map_with_index!{ |e,i| e < i ? nil : e }.compact!

before compromising with

  array = (0...array.size).zip(array).map{ |i,e| e < i ? nil : e }.compact

Anything more elegant available?

Or perhaps an alternative way to attack the problem?

Thanks,
--
http://twitter.com/bil_kleb
http://fun3d.larc.nasa.gov
Thread:
Bil Kleb
David A. Black
Bil Kleb
James Gray
Robert Klemme
James Gray
Robert Klemme
Bil Kleb
Robert Klemme
Bil Kleb

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved