Forum: Ruby Match array1 with array2

Posted by Mattias A. (mattias_a)
on 2013-02-28 06:48
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!
Posted by Carlo E. Prelz (Guest)
on 2013-02-28 07:57
(Received via mailing list)
Subject: Match array1 with array2
  Date: gio 28 feb 13 02:48:43 +0900

Quoting Mattias A. (lists@ruby-forum.com):

> 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
Posted by Robert Klemme (robert_k78)
on 2013-02-28 09:05
(Received via mailing list)
On Thu, Feb 28, 2013 at 7:49 AM, Carlo E. Prelz <fluido@fluido.as> 
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
Posted by Mattias A. (mattias_a)
on 2013-02-28 09:06
Carlo E. Prelz wrote in post #1099490:
> Subject: Match array1 with array2
>   Date: gio 28 feb 13 02:48:43 +0900
>
> Quoting Mattias A. (lists@ruby-forum.com):
>
>> 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.

http://www.ruby-doc.org/core-2.0/Array.html

Regards
Mattias
Posted by Mattias A. (mattias_a)
on 2013-02-28 19:10
Thanks for your reply robert, but don't know how to use them..
Posted by Ryan Davis (Guest)
on 2013-02-28 21:19
(Received via mailing list)
Robert told you what tools can be useful... Now... TRY them.
Posted by Sam Duncan (Guest)
on 2013-02-28 21:43
(Received via mailing list)
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
Posted by Mattias A. (mattias_a)
on 2013-03-01 08:34
Ryan Davis 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...
Posted by Mattias A. (mattias_a)
on 2013-03-01 08:56
Thanks Sam, I will try that out! :)
Posted by Josh Cheek (josh-cheek)
on 2013-03-01 15:26
(Received via mailing list)
On Fri, Mar 1, 2013 at 1:34 AM, Mattias A. <lists@ruby-forum.com> wrote:

> Ryan Davis 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?
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.