Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: inconistent test results when using is_deeply to test an array returned from a function

by APGRMF (Novice)
on Sep 25, 2014 at 10:21 UTC ( [id://1101946]=note: print w/replies, xml ) Need Help??


in reply to Re^2: inconistent test results when using is_deeply to test an array returned from a function
in thread inconistent test results when using is_deeply to test an array returned from a function

Having read/investigated a bit more, I think my original question/title was misleading - I think the issue comes down to: how do I compare two arrays for equality. Note: the equality test needs to sort the arrays first or ignore array order

  • Comment on Re^3: inconistent test results when using is_deeply to test an array returned from a function

Replies are listed 'Best First'.
Re^4: inconistent test results when using is_deeply to test an array returned from a function
by Anonymous Monk on Sep 25, 2014 at 10:54 UTC
    I think the issue comes down to: how do I compare two arrays for equality. Note: the equality test needs to sort the arrays first or ignore array order

    is_deeply is the best way to compare two data structures for equality. If the order of an array doesn't matter in the test, then it's fine to sort first: is_deeply([sort @got_files], [sort @expected_files]); (or you can sort the arrays earlier if you like). The other way to hold unsorted data would be with a hash (the difference being that an array allows duplicate values while a hash does not allow duplicate keys, and that an array will keep the order of its values while a hash will not), however is_deeply can also easily compare two hashes. So e.g. my %files = map { $_ => 1 } @files; is_deeply \%files, \%expect_files;

      Brilliant - thank you.

      I was investigating along those lines but my syntax to sort the arrays wasn't up to it, not quite sure I understand why just yet:

      is_deeply(sort(\@got_files), sort(\@expected_files), 'Function should return a list of directory contents that will be processed');

      However, your suggested syntax does the job:

      is_deeply([sort @got_files], [sort @expected_files], 'Function should return a list of directory contents that will be processed');

        When you say sort(\@got_files), sort sees a one-element list that contains an array reference. Since you want to sort the array, you have to pass sort the list, not a reference that contains it. HTH.


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found