[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array problem

John Zoldiark

12/18/2008 4:22:00 AM

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
--
Posted via http://www.ruby-....

11 Answers

Sebastian W.

12/18/2008 4:39:00 AM

0

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
--
Posted via http://www.ruby-....

Joshua Ball

12/18/2008 4:41:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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


On Wed, Dec 17, 2008 at 8:22 PM, John Zoldiark <tavarezjonathan@gmail.com>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
> --
> Posted via http://www.ruby-....
>
>

Sebastian W.

12/18/2008 4:42:00 AM

0

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.

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

Scott Lillibridge

12/18/2008 4:43:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

See Array#select
irb(main):007:0> array.select{|x| x.match(/.*\.bmp/)}
=> ["moreCoco.bmp", "theFormat.bmp"]


On Wed, Dec 17, 2008 at 9:22 PM, John Zoldiark <tavarezjonathan@gmail.com>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
> --
> Posted via http://www.ruby-....
>
>

Joshua Ball

12/18/2008 4:45:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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

On Wed, Dec 17, 2008 at 8:40 PM, Joshua Ball <chezball@gmail.com> wrote:

> 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
>
>
> On Wed, Dec 17, 2008 at 8:22 PM, John Zoldiark <tavarezjonathan@gmail.com
> >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
> > --
> > Posted via http://www.ruby-....
> >
> >
>

Christophe Mckeon

12/18/2008 4:50:00 AM

0


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

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

Tim Greer

12/18/2008 5:02:00 AM

0

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.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Joshua Ball

12/18/2008 5:03:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Arg... regex...I am new to them. ;-) You are right, of course.
Thanks (for the correction and the !~)



On Wed, Dec 17, 2008 at 8:57 PM, Tim Greer <tim@burlyhost.com> wrote:

> 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.
> --
> Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
> Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
> and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
> Industry's most experienced staff! -- Web Hosting With Muscle!
>
>

Pierre Pat

12/18/2008 5:18:00 AM

0

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...
--
Posted via http://www.ruby-....

Joshua Abbott

12/18/2008 5:32:00 AM

0

This works fine:

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

-- Josh
http://iammr...


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...

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