in reply to Matching arrays
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!#!/usr/bin/perl -w use strict; my @array1 = (1,2,2,4); my @array2 = (2,2,4,1,6); my %hash1; my %hash2; my $not_ok; foreach(@array1){ $hash1{$_} +=1; } foreach(@array2){ $hash2{$_} +=1; } while (my ($key,$hash1_value) = each %hash1) { if ($hash2{$key} && ($hash1_value == $hash2{$key})) { next; } else { $not_ok = 1; last; } } if ($not_ok) { print "Array1 is not a subset of Array2\n"; } else { print "We have a winner\n"; }
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.
|
|---|