Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Comparing arrays in perl.

by gaal (Parson)
on Aug 06, 2008 at 05:55 UTC ( #702559=note: print w/ replies, xml ) Need Help??


in reply to Comparing arrays in perl.

Your specification isn't very exact. Looks like you want two different functions. I suggest you take advantage of Perl's testing modules to specify what you want with several examples, and then play with code until all your tests pass. You can take advantage of eq_array to compare arrays. (Note: if your arrays contain references, you have more decisions to make.)

For example one of the things you're looking for seems to be to delete duplicates while preserving order. Let's call this first_filter.

use Test::More qw(no_plan); eq_array([first_filter(qw/one two three/)], [qw/one two three/], "clean list doesn't get changed"); eq_array([first_filter(qw/one one one/)], [qw/one/], "simple repetition removed"); eq_array([first_filter(qw/one two one/)], [qw/one two/], "interleaved repetition removed");

...and so on. The nice thing about this is that if I got it wrong, and this isn't the function you want, you can change the test and add more examples to clarify what you mean. When people offer you suggestions for implementation, you can with one line check their stuff and immediately see if it does everything you want it to.


Comment on Re: Comparing arrays in perl.
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://702559]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-08-20 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (107 votes), past polls