develooper Front page | perl.beginners | Postings from April 2003

Comparing arrays

Thread Next
From:
Kevin Old
Date:
April 30, 2003 08:09
Subject:
Comparing arrays
Message ID:
1051715126.2995.11.camel@localhost.localdomain
Hello everyone,

The script I wrote below does exactly what I need, but I'd be interested
in other/shorter solutions to the same problem.

Basically I just take two arrays and print the items that were in
@to_be_printed and not in @printed.

#!/usr/bin/perl -w

my @uniqto_be_printed = qw[1 2 3 4 5 6];
my @uniqprinted = qw[3 4 6 7];

my @uniqto_be_printed = &uniqArray(\@to_be_printed);
my @uniqprinted = &uniqArray(\@printed);

my %seen = ();

# We want to print all of the Invoices
foreach my $item (@uniqto_be_printed) { $seen{$item} = 1 }

# Check to see if they got printed
foreach my $item (@uniqprinted) { $seen{$item} = 2; }

# Get only keys that are in @
foreach my $key (keys %seen) {

        if($seen{$key} == 1) {
                push @uniqs, $key;
                $countuniq++;
        }
}

print "$countuniq invoices were NOT printed, they are:\n" . join("\n",
@uniqs) ."\n";

sub uniqArray {

        ($aref) = @_;

        my @uniq;
        my %seen = ();
        foreach my $item (@$aref) {
            push(@uniq, $item) unless $seen{$item}++;
        }

return @uniq;
}

Any help is appreciated!

-- 
Kevin Old <kold@carolina.rr.com>


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About