0

Having an array command port.where{port= /.+/}.ports with result:

[111, 20048, 80, 5556, 5557, 1622, 22, 23, 33080, 5432, 25, 443, 2620, 26365, 2049, 10500, 199, 29460, 7188, 4118, 55755, 35461, 51986, 123, 16515, 161, 646, 659, 13666, 29013, 611, 21460]

I need to select the value match with 2049 and 111 from the array. My code is:

port.where{port= /.+/}.ports.flatten.select {|x| x.to_i  == 111 && x.to_i == 2049}

Required result is to select only array matching values [2049,111].

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
  • 1
    Only 111 and 2049? `array.select{|e| e == 111 || e == 2049 }`? – Sebastián Palma Jun 06 '17 at 13:54
  • 2
    Take a second to think about what this says `{|x| x.to_i == 111 && x.to_i == 2049}`. Assume I handed you a deck of cards and asked you to pick the card that was a 5 and a 7. Would you be able to pick this card because that is what you are trying to select; a port that is both 111 and (`&&`) 2049. Rather as @SebastiánPalma pointed out you meant a port that is 111 or (`||`) 2049. – engineersmnky Jun 06 '17 at 13:59

0 Answers0