Match array1 with array2

Hi,

I have array1 that contains several web address. Array2 contains the
expected endings of array1 values. How can I control that arrays2 values
are in array1?

array1 = ["https://…/page.html, https://…/page1.html,
https://…/page3.html]

array2 = [“page.html”, “page2.html”, “page3.html”, “page4.html”]

A big thanks in advance!

Subject: Match array1 with array2
Date: gio 28 feb 13 02:48:43 +0900

Quoting Mattias A. ([email protected]):

I have array1 that contains several web address. Array2 contains the
expected endings of array1 values. How can I control that arrays2 values
are in array1?

array1 = ["https://…/page.html, https://…/page1.html,
https://…/page3.html]

array2 = [“page.html”, “page2.html”, “page3.html”, “page4.html”]

Use method String#end_with?

Carlo

On Thu, Feb 28, 2013 at 7:49 AM, Carlo E. Prelz [email protected]
wrote:

https://…/page3.html]

array2 = [“page.html”, “page2.html”, “page3.html”, “page4.html”]

Use method String#end_with?

… and method Enumerable#all? and probably also URI.parse, URI#path
and File.basename to extract the name of the file from the URI.

There is another more involved approach as well which involves using
method Regexp.union.

Several other solutions are also possible, especially if array2 or
array1 can be large.

Kind regards

robert

Thanks for your reply robert, but don’t know how to use them…

Robert told you what tools can be useful… Now… TRY them.

Carlo E. Prelz wrote in post #1099490:

Subject: Match array1 with array2
Date: gio 28 feb 13 02:48:43 +0900

Quoting Mattias A. ([email protected]):

I have array1 that contains several web address. Array2 contains the
expected endings of array1 values. How can I control that arrays2 values
are in array1?

array1 = ["https://…/page.html, https://…/page1.html,
https://…/page3.html]

array2 = [“page.html”, “page2.html”, “page3.html”, “page4.html”]

Use method String#end_with?

Carlo

Thanks for your reply Carlo. But how do I use end_with? on an array? I
need to find array2 values in array1 but ignore all "https://…stuff.

Regards
Mattias

On 01/03/13 07:10, Mattias A. wrote:

Thanks for your reply robert, but don’t know how to use them…

How about something like this?

irb

irb(main):001:0> array1 = [‘https://…/page.html’,
‘https://…/page1.html’, ‘https://…/page3.html’]
=> [“https://…/page.html”, “https://…/page1.html”,
“https://…/page3.html”]

irb(main):002:0> array2 = [‘page.html’, ‘page2.html’, ‘page3.html’,
‘page4.html’]
=> [“page.html”, “page2.html”, “page3.html”, “page4.html”]

irb(main):003:0> array2.select { |page| array1.any? { |address|
address.end_with? page } }
=> [“page.html”, “page3.html”]

Sam

Thanks Sam, I will try that out! :slight_smile:

Ryan D. wrote in post #1099609:

Robert told you what tools can be useful… Now… TRY them.

Hi Ryan, off course I tried them all but didn’t get the outcome that I
wanted…

On Fri, Mar 1, 2013 at 1:34 AM, Mattias A. [email protected] wrote:

Ryan D. wrote in post #1099609:

Robert told you what tools can be useful… Now… TRY them.

Hi Ryan, off course I tried them all but didn’t get the outcome that I
wanted…

What did you try? In what way did it not do what you wanted?