Forum: Ruby Array problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
06ca7bf5b4480fc2b272ab03f4c2ac3a?d=identicon&s=25 John Zoldiark (zoldiark)
on 2008-12-18 05:29
I have an array like this:
array = [ "manyThings.mp3", "myLike.gif", "moreCoco.bmp",
"theFormat.bmp"]

and I want to make some comparison to eliminate all the words in the
array that are NOT of the bmp format.  So at the end the array will look
like this:

array = ["moreCoco.bmp", "theFormat.bmp"]

is there a way to do this???

Many thanks for the help
3da512875f3295be2d577fa70b3493b4?d=identicon&s=25 Sebastian W. (globulus)
on 2008-12-18 05:47
John Zoldiark wrote:
> I have an array like this:
> array = [ "manyThings.mp3", "myLike.gif", "moreCoco.bmp",
> "theFormat.bmp"]
>
> and I want to make some comparison to eliminate all the words in the
> array that are NOT of the bmp format.  So at the end the array will look
> like this:
>
> array = ["moreCoco.bmp", "theFormat.bmp"]
>
> is there a way to do this???
>
> Many thanks for the help

Hi John,
Yep, try this:

array.reject! {|elem| elem.split(".").last != "bmp"}

Cheers,
Sebastian
3da512875f3295be2d577fa70b3493b4?d=identicon&s=25 Sebastian W. (globulus)
on 2008-12-18 05:49
And just another note: generally, for transforming and/or iterating over
arrays, finding/removing elements, extracting values, and so on, you'll
want to take a look at Ruby's "Enumerable" module. It's available on all
the main collections in Ruby and is extremely handy.

Particularly, the "each", "select", "map", "partition" and "reject"
methods are very handy. I use "map" and "select" all the time.
B15131e5df68c72103eac27911680873?d=identicon&s=25 Joshua Ball (Guest)
on 2008-12-18 05:49
(Received via mailing list)
I think this should do it:

array.delete_if { |elem| not elem =~ /.bmp/i }
It worked for the following test:
array = [ "manyThings.mp3", "myLike.gif", "moreCoco.BmP",
"theFormat.bmp"]
array.delete_if { |elem| not elem =~ /.bmp/i }
array.each do |elem|
  puts elem
end
3637c8ab02ad27d4548614027d1d26f3?d=identicon&s=25 Scott Lillibridge (Guest)
on 2008-12-18 05:52
(Received via mailing list)
See Array#select
irb(main):007:0> array.select{|x| x.match(/.*\.bmp/)}
=> ["moreCoco.bmp", "theFormat.bmp"]
B15131e5df68c72103eac27911680873?d=identicon&s=25 Joshua Ball (Guest)
on 2008-12-18 05:55
(Received via mailing list)
Actually,  the regex should have the end of string anchor:
array.delete_if { |elem| not elem =~ /.bmp$/i }
Cfb541839e42dec39f23a8a4ab8eb762?d=identicon&s=25 Christophe Mckeon (polypus)
on 2008-12-18 05:57
you might also want to anchor your regex
(the $ at the end) so that it won't work on
file names like 'foo.bmp.gz'. and be sure
to escape your . in front of bmp with \.

array.delete_if { |elem| elem !~ /\.bmp$/i }

also note the regex not match operator !~

cheers,
_c
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2008-12-18 06:06
(Received via mailing list)
Joshua Ball wrote:

>
> Actually,  the regex should have the end of string anchor:
> array.delete_if { |elem| not elem =~ /.bmp$/i }
>

Also, remember that . is any character, whereas \. is just . for the
sake of getting exactly what you want.  !~ looks cleaner to me, but
that's just personal preference in most cases.
B15131e5df68c72103eac27911680873?d=identicon&s=25 Joshua Ball (Guest)
on 2008-12-18 06:11
(Received via mailing list)
Arg... regex...I am new to them. ;-) You are right, of course.
Thanks (for the correction and the !~)
59193d8082ee0fe75675d3bb376cf2ba?d=identicon&s=25 Pierre Pierre (pierre_p)
on 2008-12-18 06:26
if you are like me an ignorant of regex, you could try this

array.delete_if { |elem| elem.index_of(".bmp") + 4 = elem.size}
though I don't have an interpreter at the moment, so it hasn't been
tested...
36aef9956c6082f69622dd67e8da376f?d=identicon&s=25 Joshua Abbott (joshnabbott)
on 2008-12-18 06:39
This works fine:

array.select { |item| item =~ /\.bmp/ }

-- Josh
http://iammrjoshua.com


Pierre Pat wrote:
> if you are like me an ignorant of regex, you could try this
>
> array.delete_if { |elem| elem.index_of(".bmp") + 4 = elem.size}
> though I don't have an interpreter at the moment, so it hasn't been
> tested...
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2008-12-18 10:44
(Received via mailing list)
John Zoldiark wrote:
> I have an array like this:
> array = [ "manyThings.mp3", "myLike.gif", "moreCoco.bmp",
> "theFormat.bmp"]
>
> and I want to make some comparison to eliminate all the words in the
> array that are NOT of the bmp format.  So at the end the array will look
> like this:
>
> array = ["moreCoco.bmp", "theFormat.bmp"]

Instead of a reject/select you can also use grep when working with
regexen:
array.grep(/\.bmp$/)
Instead of using a regex you could also use File.extname here:
array.select {|x| File.extname(x)==".bmp"}
Of course all these only tell you that the filenames end with .bmp, not
in
which format the files actually are.

HTH,
Sebastian
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2008-12-18 11:53
(Received via mailing list)
array.grep /bmp$/
=> ["moreCoco.bmp", "theFormat.bmp"]
This topic is locked and can not be replied to.