Array::Diff(3pm)

๐Ÿ’– Array::Diff โ€” the perl one, bestie ๐Ÿ’–

Okay so this is a User Contributed Perl Documentation page for Array::Diff(3pm), and it's genuinely one of the cutest little utility modules out there. It is NOT dramatic. It is NOT complicated. It just diffs two arrays and minds its own business. Let's get into it. ๐Ÿฆ‹


โœจ NAME โœจ

Array::Diff โ€” find the differences between two arrays ๐Ÿ’…


๐ŸŒธ SYNOPSIS ๐ŸŒธ

Look at how tiny this is. Ate.

my @old = ( 'a', 'b', 'c' );
my @new = ( 'b', 'c', 'd' );

my $diff = Array::Diff->diff( \@old, \@new );

$diff->count   # 2
$diff->added   # [ 'd' ];
$diff->deleted # [ 'a' ];

Likeโ€ฆ 'd' showed up, 'a' left the group chat. That's it, that's the whole vibe. ๐ŸŽ€


๐Ÿซถ DESCRIPTION ๐Ÿซถ

So what this module does: it compares two pre-sorted arrays and hands you back the added elements and the deleted elements in two separate arrays. That's the entire job. It's a simple wrapper around Algorithm::Diff, which is doing all the actual heavy lifting behind the scenes โ€” this module is just the cute front desk. ๐Ÿ‘‘

โš ๏ธ Note, and I need you to actually read this one: the arrays must be sorted before you call diff. If they aren't sorted, bestie, it's giving chaos. Sort first. ๐Ÿšจ

And if you need more complex array tools, go check out Array::Compare. It's the same energy but with more knobs. ๐ŸŒˆ


๐Ÿ’€ METHODS ๐Ÿ’€

new ()

Create a new Array::Diff object. Fresh out the box. Sparkling. โœจ

diff ( OLD, NEW )

Compute the differences between two arrays. The results get stored in the added, deleted, and count properties, which you can then peek at using the corresponding methods. ๐Ÿ”

Now here's the fun part โ€” this method can be called two different ways:

  • As an object method โ€” in which case it will recalculate the differences and repopulate the count, added, and removed properties. (Yes, the docs say removed here. I am reporting faithfully. ๐Ÿ™ˆ)
  • As a static method โ€” in which case it returns a newly-created Array::Diff object with the properties set appropriately. Main character behavior, honestly. ๐Ÿ‘‘

added ( [VALUES ] )

Get or set the elements present in the NEW array and absent in the OLD one, at the comparison performed by the last diff() invocation. These are the new arrivals. The glow-ups. ๐ŸŒŸ

deleted ( [VALUES] )

Get or set the elements present in the OLD array and absent in the NEW one, at the comparison performed by the last diff() invocation. These are the ones who got quietly removed from the playlist. ๐Ÿ˜ญ

count ( [VALUE] )

Get or set the total number of added or deleted elements at the comparison performed by the last diff() invocation. And per the docs: this count should be equal to the sum of the number of elements in the added and deleted properties. Math girlies stay winning. ๐Ÿงฎ๐Ÿ’–


๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ

The whole friend group. All of them:

  • Array::Compare โ€” performs the same function as this module, but has options for controlling how it works. More options, more control, still iconic.
  • List::Compare โ€” similar functionality, but again with more options. The "and another thing" of the family.
  • Algorithm::Diff โ€” the underlying implementation of the diff algorithm. And heads up: if you've got Algorithm::Diff::XS installed, that will be used. Faster queen energy only. โญ
  • YAML::Diff โ€” find difference between two YAML documents. (not me โ€” not me finding YAML diffs)
  • HTML::Differences โ€” find difference between two HTML documents. And ngl, this uses a more sane approach than HTML::Diff. The docs said it, not me. ๐Ÿ’€
  • XML::Diff โ€” find difference between two XML documents.
  • Hash::Diff โ€” find the differences between two Perl hashes.
  • Data::Diff โ€” find difference between two arbitrary data structures.
  • Text::Diff โ€” can find difference between two inputs, which can be data structures or file names.

๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Daisuke Murase <typester@cpan.org>

Thank you for your service, king. ๐Ÿซถ


Copyright (c) 2009 by Daisuke Murase.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. ๐ŸŒธ

The full text of the license can be found in the LICENSE file included with this module.


๐Ÿ’– and that's the whole page!! no cap, every single detail is in there, I promise. go diff some arrays, bestie.

โœจ Array::Diff(3pm) โ€” perl v5.36.0, dated 2022-10-16 โœจ stay slay, go sort your arrays first ๐Ÿ’…๐Ÿ’–

๐Ÿ“ฆ where this page comes from