Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > move to front of array

Reply
Thread Tools

move to front of array

 
 
Payton Swick
Guest
Posts: n/a
 
      12-21-2005
Hi,

I have an array of Strings, and I'd like to find one of the items by
Regexp, then move that item to the front of the array, eg:

array = %w(a B c d Cool e f G)
array.unshift(array.slice!(array.index(array.find { |i| i =~ /cool/i
}))) if array.find { |i| i =~ /cool/i }

Better/cleaner/shorter ways to do it?

-Payton


 
Reply With Quote
 
 
 
 
Florian Frank
Guest
Posts: n/a
 
      12-21-2005
Payton Swick wrote:

> I have an array of Strings, and I'd like to find one of the items by
> Regexp, then move that item to the front of the array, eg:
>
> array = %w(a B c d Cool e f G)
> array.unshift(array.slice!(array.index(array.find { |i| i =~ /cool/i
> }))) if array.find { |i| i =~ /cool/i }



In 1.8.x:

require 'enumerator'
if f = array.enum_for(:each_index).find { |i| array[i] =~ /cool/i }
array.unshift array.delete_at(f)
end


In 1.9.x you can do:

array.unshift array.delete_at(array.index { |x| x =~ /cool/i })

--
Florian Frank



 
Reply With Quote
 
 
 
 
Jacob Fugal
Guest
Posts: n/a
 
      12-21-2005
On 12/21/05, Payton Swick <(E-Mail Removed)> wrote:
> Hi,
>
> I have an array of Strings, and I'd like to find one of the items by
> Regexp, then move that item to the front of the array, eg:
>
> array =3D %w(a B c d Cool e f G)
> array.unshift(array.slice!(array.index(array.find { |i| i =3D~ /cool/i
> }))) if array.find { |i| i =3D~ /cool/i }
>
> Better/cleaner/shorter ways to do it?
>
> -Payton


array =3D %w(a B c d Cool e f G)
if cool =3D array.find { |i| i =3D~ /cool/i }
array.delete(cool)
array.unshift(cool)
end

?

Jacob Fugal


 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      12-21-2005

On Dec 21, 2005, at 3:08 PM, Payton Swick wrote:

> Hi,
>
> I have an array of Strings, and I'd like to find one of the items
> by Regexp, then move that item to the front of the array, eg:
>
> array = %w(a B c d Cool e f G)
> array.unshift(array.slice!(array.index(array.find { |i| i =~ /cool/
> i }))) if array.find { |i| i =~ /cool/i }
>
> Better/cleaner/shorter ways to do it?
>
> -Payton
>


I don't know about shorter but...

require 'enumerator'
array = %w(a B c d Cool e f G)
index = array.to_enum(:each_index).find { |i| array[i] =~ /cool/i }
array.unshift( array.delete_at( index ) )



 
Reply With Quote
 
Eero Saynatkari
Guest
Posts: n/a
 
      12-21-2005
Payton Swick wrote:
> Hi,
>
> I have an array of Strings, and I'd like to find one of the items by
> Regexp, then move that item to the front of the array, eg:
>
> array = %w(a B c d Cool e f G)
> array.unshift(array.slice!(array.index(array.find { |i| i =~ /cool/i
> }))) if array.find { |i| i =~ /cool/i }
>
> Better/cleaner/shorter ways to do it?


This should work (but without error checking):

array.unshift(array.delete(array.grep(/cool/i).first))

> -Payton



E


--
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
Dan Diebolt
Guest
Posts: n/a
 
      12-21-2005
--0-302192896-1135198321=:86581
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Try this:
=20
array1 =3D %w(a B c d Cool e f G)
array1.partition {|i| i =3D~ /cool/i}
array2=3Darray1.partition {|i| i =3D~ /cool/i}
array2.flatten #or array2.flatten!

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20
--0-302192896-1135198321=:86581--


 
Reply With Quote
 
Ezra Zygmuntowicz
Guest
Posts: n/a
 
      12-21-2005

On Dec 21, 2005, at 12:08 PM, Payton Swick wrote:

> Hi,
>
> I have an array of Strings, and I'd like to find one of the items
> by Regexp, then move that item to the front of the array, eg:
>
> array = %w(a B c d Cool e f G)
> array.unshift(array.slice!(array.index(array.find { |i| i =~ /cool/
> i }))) if array.find { |i| i =~ /cool/i }
>
> Better/cleaner/shorter ways to do it?
>
> -Payton
>


array.unshift(array.delete_at(array.index("Cool")) )

# => ["Cool", "a", "B", "c", "d", "e", "f", "G"]

-Ezra


 
Reply With Quote
 
Jacob Fugal
Guest
Posts: n/a
 
      12-21-2005
On 12/21/05, Dan Diebolt <(E-Mail Removed)> wrote:
> Try this:
>
> array1 =3D %w(a B c d Cool e f G)
> array1.partition {|i| i =3D~ /cool/i}
> array2=3Darray1.partition {|i| i =3D~ /cool/i}
> array2.flatten #or array2.flatten!


So something like:

array =3D %w(a B c d Cool e f G)
array =3D array.partition{ |el| el =3D~ /cool/i }.flatten

That certainly would work (assuming that only one element matches the
condition), and has a certain elegance. I wonder if there's a
significant performance penalty?

It also gave me this idea:

class Array
def bubble_up
score =3D lambda{ |a| (yield a) ? -1 : 1 }
self.sort_by { |a,b| score[a] <=3D> score[b] }
end

def bubble_up!( &proc )
self.replace(self.bubble_up( &proc ))
end
end

array =3D array.bubble_up! { |el| el =3D~ /cool/i }

Jacob Fugal


 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      12-21-2005

Jacob Fugal wrote:
> On 12/21/05, Payton Swick <(E-Mail Removed)> wrote:
> > Hi,
> >
> > I have an array of Strings, and I'd like to find one of the items by
> > Regexp, then move that item to the front of the array, eg:
> >
> > array = %w(a B c d Cool e f G)
> > array.unshift(array.slice!(array.index(array.find { |i| i =~ /cool/i
> > }))) if array.find { |i| i =~ /cool/i }
> >
> > Better/cleaner/shorter ways to do it?
> >
> > -Payton

>
> array = %w(a B c d Cool e f G)
> if cool = array.find { |i| i =~ /cool/i }
> array.delete(cool)
> array.unshift(cool)
> end


array.unshift(array.delete("Cool"))

Regards,

Dan

 
Reply With Quote
 
Dan Diebolt
Guest
Posts: n/a
 
      12-21-2005
--0-840281129-1135201148=:26597
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

>That certainly would work (assuming that only one element matches the

condition),
=20
seems to work on multiple matches:
=20
array =3D %w(a B c d Cool e f G Cool Cooler)
=3D> ["a", "B", "c", "d", "Cool", "e", "f", "G", "Cool", "Cooler"]
array =3D array.partition{ |el| el =3D~ /cool/i }.flatten!
=3D> ["Cool", "Cool", "Cooler", "a", "B", "c", "d", "e", "f", "G"]

I was uncertain about needing parenthesis before applying flatten but it =
makes perfect sense:
=20
array =3D (array.partition{ |el| el =3D~ /cool/i }).flatten!

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20
--0-840281129-1135201148=:26597--


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing move constructors and move assignment Andrew Tomazos C++ 2 12-12-2011 01:45 PM
How to get string from front of array to position x? Angus C Programming 3 01-22-2011 08:27 PM
What data structure for a move-to-front coder? Tom Anderson Java 2 03-11-2009 03:15 AM
Move Array entry up or down in the Array David Javascript 5 11-08-2006 05:45 PM
I am trying to move spaces to a weblistbox and when I move them... Eduardo78 ASP .Net Web Controls 0 11-03-2005 06:06 PM



Advertisments