in reply to How to find and print duplicte elemets in multi dimensional array

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

Replies are listed 'Best First'.
Re^2: How to find and print duplicte elemets in multi dimensional array
by techlearner (Initiate) on Apr 27, 2011 at 05:07 UTC
    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.
Re^2: How to find and print duplicte elemets in multi dimensional array
by Anonymous Monk on Apr 26, 2011 at 12:08 UTC
    Many Thanks, this logic is very simple and it is working.