I need to compare arrays in perl. Now the arrays are arrays of strings. The strings are similar to the following pattern:


There are 4 machines.


There are 4 arrays and each with a bunch of users. some users appear on multiple machines. I need to look for any usernames that appear multiple times and eliminate the extras. the one that should remain is the one with the highest priority.


if ben has accounts on M2 and M4, then eliminate ben:ben@M4.domain.com.

7 Years
Discussion Span
Last Post by d5e5

I think I would start by combining all four arrays into one array. Then sort it and the highest priority element for each user will be at the top of each group of elements starting with that user.

Then I would loop through the sorted array, printing only the first element for each user.

#!/usr/bin/perl -w
use strict;
my @arr1 = qw(susan:susan@M1.domain.com ben:ben@M1.domain.com carol:carol@M1.domain.com carol:carol@M1.domain.com ben:ben@M1.domain.com);
my @arr2 = qw(carol:carol@M2.domain.com ben:ben@M2.domain.com ben:ben@M2.domain.com susan:susan@M2.domain.com ted:ted@M2.domain.com);
my @arr3 = qw(susan:susan@M3.domain.com ted:ted@M3.domain.com ben:ben@M3.domain.com susan:susan@M3.domain.com ben:ben@M3.domain.com);
my @arr4 = qw(susan:susan@M4.domain.com ben:ben@M4.domain.com alice:alice@M4.domain.com susan:susan@M4.domain.com alice:alice@M4.domain.com);
my @arrall = sort(@arr1, @arr2, @arr3, @arr4); #Combine all four arrays and sort the elements
my $wholestring;
my ($user,$saved) = ("","");
foreach (@arrall) {
    $wholestring = $&;
    $user = $1;
    if ($user ne $saved) {
        print "$wholestring\n";
        $saved = $user;

Thanks for the reply. I was able to work around comparing arrays. I used maps.

Maps is a <KEY><VALUE> pair. If you set the USERNAME as the key and MACHINE_NAME as the value, then when you add a username that already exist, the value gets updated.

Start with the lowest priority machine, so that when you're done, the map will contain the machine with the highest priority and not something else.

Ideally I would have liked to use regular expressions to compare the arrays. However this works too. I just worked around it. I am horrible at regular expressions, that is something I need to read on during the Christmas break!



Some people, when confronted with a problem, think “I know,
I'll use regular expressions.” Now they have two problems. ~Jamie Zawinski (famous Netscape engineer who didn't like Perl)

One thing I like about Perl is that it has regular expressions support built into it, so you don't have to use special modules and objects to use RegEx. When I used to work in IT, using regular expressions in a text editor saved me lots of time reformatting data. At the time I knew nothing about Perl and only a little about regular expressions, but my colleagues knew even less about them and thought I was performing miracles. Those were the days.

Your solution sounds like it works fine. Regular expressions are not the best tool for every situation but sometimes they are very handy.:)

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.