Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2196 > unrolled thread

Using grep on subarrays - help!

Started bySimon Harrison <simon@simonharrison.net>
First post2011-04-03 08:32 -0500
Last post2011-04-04 15:56 -0500
Articles 12 — 6 participants

Back to article view | Back to comp.lang.ruby


Contents

  Using grep on subarrays - help! Simon Harrison <simon@simonharrison.net> - 2011-04-03 08:32 -0500
    Re: Using grep on subarrays - help! Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-03 08:47 -0500
      Re: Using grep on subarrays - help! Adam Prescott <adam@aprescott.com> - 2011-04-03 09:44 -0500
        Re: Using grep on subarrays - help! Robert Klemme <shortcutter@googlemail.com> - 2011-04-04 04:13 -0500
    Re: Using grep on subarrays - help! Simon Harrison <simon@simonharrison.net> - 2011-04-03 08:58 -0500
    Re: Using grep on subarrays - help! 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-03 16:13 -0500
      Re: Using grep on subarrays - help! 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-03 16:49 -0500
    Re: Using grep on subarrays - help! Simon Harrison <simon@simonharrison.net> - 2011-04-04 14:06 -0500
      Re: Using grep on subarrays - help! Rob Biedenharn <Rob@AgileConsultingLLC.com> - 2011-04-04 14:44 -0500
      Re: Using grep on subarrays - help! 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-04 18:41 -0500
    Re: Using grep on subarrays - help! Simon Harrison <simon@simonharrison.net> - 2011-04-04 15:05 -0500
      Re: Using grep on subarrays - help! Rob Biedenharn <Rob@AgileConsultingLLC.com> - 2011-04-04 15:56 -0500

#2196 — Using grep on subarrays - help!

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-03 08:32 -0500
SubjectUsing grep on subarrays - help!
Message-ID<dd95c6e0e47f0378352ab56c23664c9c@ruby-forum.com>
Can anyone help with this? I thought grep would find any element that
matches in an array. It seems not...


irb(main):028:0> test = [['one', 'vol1'], ['one', 'vol2'], ['two',
'vol3']]
=> [["one", "vol1"], ["one", "vol2"], ["two", "vol3"]]
irb(main):029:0> test.grep(/one/)
=> []
irb(main):030:0> test.each.grep(/one/)
=> []
irb(main):031:0> test
=> [["one", "vol1"], ["one", "vol2"], ["two", "vol3"]]
irb(main):032:0> test.grep('one')
=> []
irb(main):033:0> test2 = ['one', 'one', 'two', 'three']
=> ["one", "one", "two", "three"]
irb(main):034:0> test2.grep(/one/)
=> ["one", "one"]
irb(main):035:0>

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

[toc] | [next] | [standalone]


#2197

FromJesús Gabriel y Galán <jgabrielygalan@gmail.com>
Date2011-04-03 08:47 -0500
Message-ID<BANLkTimbxgm2vcchEFTzNbBJhOgkTP=mvg@mail.gmail.com>
In reply to#2196
On Sun, Apr 3, 2011 at 3:32 PM, Simon Harrison <simon@simonharrison.net> wrote:
> Can anyone help with this? I thought grep would find any element that
> matches in an array. It seems not...
>
>
> irb(main):028:0> test = [['one', 'vol1'], ['one', 'vol2'], ['two',
> 'vol3']]
> => [["one", "vol1"], ["one", "vol2"], ["two", "vol3"]]
> irb(main):029:0> test.grep(/one/)
> => []

test.each {|x| puts x.grep /one/}

=> one
one

If you want to choose all pairs that match:

result = []
test.each {|x| result << x unless x.grep(/one/).empty?}
result

=> [["one", "vol1"],["one","vol2"]]

Jesus.

[toc] | [prev] | [next] | [standalone]


#2199

FromAdam Prescott <adam@aprescott.com>
Date2011-04-03 09:44 -0500
Message-ID<AANLkTin+GUSP0_ZPHyp_uW+XxA72HUK4vZb6MN23ZZMt@mail.gmail.com>
In reply to#2197
2011/4/3 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>

> result = []
> test.each {|x| result << x unless x.grep(/one/).empty?}
> result
>

More to-the-point:

test.reject { |x| x.grep(/one/).empty? }

[toc] | [prev] | [next] | [standalone]


#2245

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-04 04:13 -0500
Message-ID<BANLkTimNcQYB6kotGT-Vre1DoV8QpJXEsw@mail.gmail.com>
In reply to#2199
On Sun, Apr 3, 2011 at 4:44 PM, Adam Prescott <adam@aprescott.com> wrote:
> 2011/4/3 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>
>
>> result = []
>> test.each {|x| result << x unless x.grep(/one/).empty?}
>> result
>>
>
> More to-the-point:
>
> test.reject { |x| x.grep(/one/).empty? }

It seems we may want to match at the first position only, so I'd
rather do one of those

irb(main):003:0> test.select {|a,b| /one/ =~ a}
=> [["one", "vol1"], ["one", "vol2"]]
irb(main):004:0> test.select {|a,b| a == "one"}
=> [["one", "vol1"], ["one", "vol2"]]

If we are only interested in the other bit

irb(main):005:0> test.select {|a,b| a == "one"}.map {|a,b| b}
=> ["vol1", "vol2"]

Or, with #inject for a change since we know the key when doing exact matches

irb(main):006:0> test.inject([]) {|r,(a,b)| a == "one" ? r << b : r}
=> ["vol1", "vol2"]

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#2198

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-03 08:58 -0500
Message-ID<9fe86f0c781a99fdfcb0143b8de20e13@ruby-forum.com>
In reply to#2196
Thanks again Jesus. Is it just me or does that seem a bit longwinded. 
Any idea why one can't just directly an array with subarrays?

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

[toc] | [prev] | [next] | [standalone]


#2223

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-03 16:13 -0500
Message-ID<ae03f9731d52ff2aab147cfc64bcd8d2@ruby-forum.com>
In reply to#2196
Simon Harrison wrote in post #990675:
> Can anyone help with this? I thought grep would find any element that
> matches in an array. It seems not...
>

You are trying to use grep() to match strings, yet your test array does 
not contain strings--it contains sub-arrays.   So first you have to grab 
the sub-arrays, and then you can apply grep to the sub-arrays because 
they contain strings.

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

[toc] | [prev] | [next] | [standalone]


#2228

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-03 16:49 -0500
Message-ID<b373f8b1bc1226c9c1e9009060bfc1e8@ruby-forum.com>
In reply to#2223
7stud -- wrote in post #990727:
>
> new_arr = test.select do |arr|
>   if arr.include?(target)
>     true
>   end
> end
>

I guess that can be written more succinctly as:

new_arr = test.select do |arr|
  arr.include?(target)
end

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

[toc] | [prev] | [next] | [standalone]


#2278

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-04 14:06 -0500
Message-ID<38d8baa728f8f62120993c3f49b2c110@ruby-forum.com>
In reply to#2196
Thanks for all the tips. I think #select fits best:

file_results = @films.select { |a, b| /#{@film}/i =~ a }


Just one question. Obviously in the above, a and b refer to the first 
and second elements in each subarray. Let's say we have this array:

=> [["one", "two", "three"], ["one"], ["two", "three"], ["one", 
"three"]]

What would be the best way to search for 'one' (or whatever) in this 
case?

Cheers

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

[toc] | [prev] | [next] | [standalone]


#2279

FromRob Biedenharn <Rob@AgileConsultingLLC.com>
Date2011-04-04 14:44 -0500
Message-ID<1CA237B4-B43D-4137-9B07-8DC806BE7516@AgileConsultingLLC.com>
In reply to#2278
On Apr 4, 2011, at 3:06 PM, Simon Harrison wrote:

> Thanks for all the tips. I think #select fits best:
>
> file_results = @films.select { |a, b| /#{@film}/i =~ a }
>
>
> Just one question. Obviously in the above, a and b refer to the first
> and second elements in each subarray. Let's say we have this array:
>
> => [["one", "two", "three"], ["one"], ["two", "three"], ["one",
> "three"]]
>
> What would be the best way to search for 'one' (or whatever) in this
> case?
>
> Cheers
>
> --  
> Posted via http://www.ruby-forum.com/.
>

irb> @films = [["one", "two", "three"], ["one"], ["two", "three"],  
["one", "three"]]
=> [["one", "two", "three"], ["one"], ["two", "three"], ["one",  
"three"]]
irb> @film = 'One'
=> "One"
irb> @films.select{|film,*_| /#{@film}/i =~ film}
=> [["one", "two", "three"], ["one"], ["one", "three"]]

_ is a valid identifier and I tend to use it to mean either "a  
throwaway placeholder" (like in this usage) or "a quick, single block  
arg" (as in arry.map{|_|_.something} to mean the same as  
arry.map(&:something) without needing Symbol#to_proc).


-Rob

Rob Biedenharn		
Rob@AgileConsultingLLC.com	http://AgileConsultingLLC.com/
rab@GaslightSoftware.com		http://GaslightSoftware.com/

[toc] | [prev] | [next] | [standalone]


#2285

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-04 18:41 -0500
Message-ID<3fcc2d1bc9b4ca54a656e2dd1db9702f@ruby-forum.com>
In reply to#2278
Simon Harrison wrote in post #990886:
> Thanks for all the tips. I think #select fits best:
>
> file_results = @films.select { |a, b| /#{@film}/i =~ a }
>
>
> Just one question. Obviously in the above, a and b refer to the first
> and second elements in each subarray. Let's say we have this array:
>
> => [["one", "two", "three"], ["one"], ["two", "three"], ["one",
> "three"]]
>

Neither grep() or include?() depended on the size of the sub-arrays they 
are searching, so the answer is the same:


data = [
  ["one", "two", "three"],
  ["one"],
  ["two", "three"],
  ["one","three"]
]

target = 'one'

results = data.select do |arr|
  arr.include?(target)
end

p results

--output:--
[["one", "two", "three"], ["one"], ["one", "three"]]

However, if your target will only appear in the first position of the 
array, then it is much more efficient to just check the first element of 
each array:

data = [
  ["one", "two", "three"],
  ["ONE"],
  ["two", "three"],
  ["oNe","three"]
]

target = 'one'

results = data.select do |arr|
  arr[0].downcase == target
end

p results

--output:--
[["one", "two", "three"], ["ONE"], ["oNe", "three"]]

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

[toc] | [prev] | [next] | [standalone]


#2281

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-04 15:05 -0500
Message-ID<8573756b7caaa4e23405d873e41fe060@ruby-forum.com>
In reply to#2196
Thanks Rob,very useful. Any chance of expanding on this at all:

"a quick, single block
arg" (as in arry.map{|_|_.something} to mean the same as
arry.map(&:something) without needing Symbol#to_proc)."

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

[toc] | [prev] | [next] | [standalone]


#2283

FromRob Biedenharn <Rob@AgileConsultingLLC.com>
Date2011-04-04 15:56 -0500
Message-ID<B9C9977D-801E-42FE-9B96-74D0D262A878@AgileConsultingLLC.com>
In reply to#2281
On Apr 4, 2011, at 4:05 PM, Simon Harrison wrote:

> Thanks Rob,very useful. Any chance of expanding on this at all:
>
> "a quick, single block
> arg" (as in arry.map{|_|_.something} to mean the same as
> arry.map(&:something) without needing Symbol#to_proc)."
>
> --  
> Posted via http://www.ruby-forum.com/.
>



Here's a little example from an error handler in a script (first  
occurrence in an open buffer):

   rescue => e
     if $stderr.isatty
       $stderr.puts e.message
       $stderr.puts e.backtrace.select {|_| %r{/app/} =~ _}.join("\n\t")
     else
       raise
     end
   end

While I could replace the "_" with a name like "line" or  
"backtrace_line", that doesn't really increase the expressiveness when  
the entire block is one statement/expression.

-Rob

Rob Biedenharn		
Rob@AgileConsultingLLC.com	http://AgileConsultingLLC.com/
rab@GaslightSoftware.com		http://GaslightSoftware.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web