in reply to tweaking of perldoc function (sorting/preserving order)

#!/usr/bin/perl use strict; use warnings; use List::Compare; use Data::Dumper; my @array1 = (1,2,3,4,5); my @array2 = (4,5,6,7,8); #my $lc = List::Compare->new( '-u', \@array1, \@array2); my $lc = List::Compare->new( \@array1, \@array2); my $compare = { 'Union' => [ $lc->get_union ], 'Intersection' => [ $lc->get_intersection ], 'Difference' => [ $lc->get_unique ], 'Sym_diff' => [ $lc->get_symmetric_difference ] }; $Data::Dumper::Indent = 1; print Dumper($compare);

Output:
$VAR1 = { 'Intersection' => [ '4', '5' ], 'Sym_diff' => [ '1', '2', '3', '6', '7', '8' ], 'Difference' => [ '1', '2', '3' ], 'Union' => [ '1', '2', '3', '4', '5', '6', '7', '8' ] };

HTH,

PooLpi

Replies are listed 'Best First'.
Re^2: tweaking of perldoc function (sorting/preserving order)
by lodin (Hermit) on Jan 18, 2008 at 20:47 UTC

    List::Compare doesn't necessarily preserve any order. The default behaviour is simply to sort the lists by Perl's default sort order. Without sorting no promise about ordering is given. Keeping original order is what the OP asks for (dispite the title), so List::Compare is unfortunately not a reliable solution.

    lodin