arrow has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Matching arrays
by robartes (Priest) on Mar 22, 2003 at 12:55 UTC | |
Note that there are some interesting points on array manipulations like this in perlfaq4. CU | [reply] [d/l] |
|
Re: Matching arrays
by gmax (Abbot) on Mar 22, 2003 at 13:40 UTC | |
Using a join is much faster. update (1). Provided that the arrays are both sorted and contiguous. Thanks robartes for pointing it out. update (2). A modified routine, also using join solves both the above problems. (see below)
update (2). This modified sub can also find a match when the arrays are not sorted, with almost the same efficiency.
| [reply] [d/l] [select] |
|
Re: Matching arrays
by demerphq (Chancellor) on Mar 22, 2003 at 13:13 UTC | |
Assuming they are both sorted lists this will find if @$s is in @$a
But generally you will want to do this lookup many times on @$a so in that case you should build a hash. --- demerphq <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
• Update: | [reply] [d/l] [select] |
|
Re: Matching arrays
by Limbic~Region (Chancellor) on Mar 22, 2003 at 15:56 UTC | |
I think you left out some important information. As BrowswerUK pointed out - are you looking for a match if the sequence doesn't match, but all the elements are present? Is a match acceptable if the same element is repeated in one array, but is only present once in the other? If you have a requirement to match duplicates than converting to a hash without a count is not going to work.
There is a penalty in efficiency for wanting this kind of accuracy - thanks to gmax for pointing this out and my solution using Data::Dumper was just plain wrong! Cheers - L~R
Update: If sequence is important, then I would use the following logic:
There is also a module that can preserve the order of a tied hash through some unknown magic you could look at, but I don't know much about it. | [reply] [d/l] |
|
Re: Matching arrays
by zby (Vicar) on Mar 22, 2003 at 13:20 UTC | |
Update: Ofcourse you need intersection and equality. | [reply] [d/l] |
|
Re: Matching arrays
by BrowserUk (Patriarch) on Mar 22, 2003 at 13:14 UTC | |
Would you consider it a match if array 1 contained (4,5,6) and array 2 contained (1, 2, 3, 6, 4, 5) ? Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong. 2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible 3) Any sufficiently advanced technology is indistinguishable from magic. Arthur C. Clarke. | [reply] |
|
Re: Matching arrays
by arrow (Friar) on Mar 24, 2003 at 15:37 UTC | |
| [reply] [d/l] [select] |
|
Re: Matching arrays
by Anonymous Monk on Mar 26, 2003 at 07:43 UTC | |
Edit by tye, add CODE tags | [reply] [d/l] |
by Anonymous Monk on Mar 26, 2003 at 08:03 UTC | |
Edit by tye, add CODE tags | [reply] [d/l] |