techlearner has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am beginner in PERL , I am trying to write a script which will find duplicate elements in multi dimensional array.

My array is o/p of a backup jobs summary which contains device names and backup polices, I want to find duplicate jobs with respect to devices and backup policy names simply finding jobs which have same device name and same policy name.

First I sorted the array with device names then tried to compare each device policy name with it's next row and print row whih are whaich has same elements for device na dpolicy.

Her is code for sorting w.r.t device name which is 7th element

#!/usr/bin/perl while (<>) { @tmp = split(/,/); push @failures, [ @tmp ]; } @sorted = sort { $a->[6] cmp $b->[6] } @failures; for $array_ref ( @sorted ) { print "@$array_ref \n" ;

o/p of this script goest another script which finds duplicate devic enad policy names 5th elemt is policy name

#!/usr/bin/perl while (<>) { @tmp = split; push @failures, [ @tmp ]; } $konst = $failures[0]; for $i ( 0 .. $#failures ){ for $j ( 0 .. 6 ) { if ( $i == 0 ) { if ( $konst->[4] eq $failures[$i+1][4] && $konst->[6] eq $failure +s[$i+1][6] ) { print "\t$failures[$i][$j]"; $konst = $failures[$i+1]; ; } } elsif ( $konst->[6] eq $failures[$i+1][6] && $konst->[6] eq $failures[ +$i+1][6] ) { print "\t$failures[$i][$j]"; $konst = $failures[$i+1]; ;} } print "\n";
}

but I ma not getting desired o/p I feel logic is correct please help me or guide if there is any better way of doing it in PERL

File contents are like this, it is example how each line looks like in after sorting w.r.t device name

4778228,0,0,,Policy_name,Incremental,Device_name, ,1291910125,0000000053,0000000000, ,1 ,,,,,0,,root,0,13,1,99998,root,x,1,30,0,0,0,0,0,4778228,,,,,,,,,,,,,0,0,1,0,0,,0,,,,Max

Thanks, TechLearner

Replies are listed 'Best First'.
Re: How to find and print duplicte elemets in multi dimensional array
by moritz (Cardinal) on Dec 13, 2010 at 15:06 UTC
    If you search for duplicates, think hash. A hash can store zero or one items with a given key.

    If you store your data in a hash with the thing you want to compare on as the key, finding duplicates is a simple matter of asking for the hash element before you store something. If it already exists, the current occurence is a duplicate.

    See also: perlintro, perldata.

Re: How to find and print duplicte elemets in multi dimensional array
by VinsWorldcom (Prior) on Dec 13, 2010 at 15:28 UTC

    To add to moritz suggestion of a hash above, couldn't you do the whole thing in just one script? You don't need to sort if you use a hash so it becomes much simplier.

    For example:

    #!/usr/bin/perl use strict; use warnings; my %dup; while (<DATA>) { my @tmp = split /,/; if (exists($dup{$tmp[6]}{$tmp[4]})) { print "DUPLICATE!\n Device: $tmp[6]\n Policy: $tmp[4]\n\n" } else { $dup{$tmp[6]}{$tmp[4]} = 1 } } __DATA__ 4778228,0,0,,Policy_name1,Incremental,Device_name1, ,1291910125,000000 +0053,0000000000, ,1 ,,,,,0,,root,0,13,1,99998,root,x,1,30,0,0,0,0,0,4 +778228,,,,,,,,,,,,,0,0,1,0,0,,0,,,,Max 4778228,0,0,,Policy_name2,Incremental,Device_name2, ,1291910125,000000 +0053,0000000000, ,1 ,,,,,0,,root,0,13,1,99998,root,x,1,30,0,0,0,0,0,4 +778228,,,,,,,,,,,,,0,0,1,0,0,,0,,,,Max 4778228,0,0,,Policy_name3,Incremental,Device_name3, ,1291910125,000000 +0053,0000000000, ,1 ,,,,,0,,root,0,13,1,99998,root,x,1,30,0,0,0,0,0,4 +778228,,,,,,,,,,,,,0,0,1,0,0,,0,,,,Max 4778228,0,0,,Policy_name2,Incremental,Device_name2, ,1291910125,000000 +0053,0000000000, ,1 ,,,,,0,,root,0,13,1,99998,root,x,1,30,0,0,0,0,0,4 +778228,,,,,,,,,,,,,0,0,1,0,0,,0,,,,Max

    Output:

    C:\> test.pl DUPLICATE! Device: Device_name2 Policy: Policy_name2
      Many Thanks, this code is working and it is very simple. I am a beginner and I am approaching PERL on problem basis , I needed a script to find duplicates so I thought Perl can do that and tried create script for that, I have exposure to C and java in my college so I though Arrays can do the job so started script on that but Hashes are much more powerful and simple in code and it simply shows how powerful PERL is. Hopefully in future I will work more on PERL and will get to know much more about this simple and powerful language. Thanks again.
      Many Thanks, this logic is very simple and it is working.