Hi Guys! I am very new to perl I have similar kind of problem where I need to compare 2 files , lets say : file1 contents= M|_|03-04-2012_08:21:03|_|Device1|_|8|_|Int/0.101|_| M|_|03-04-2012_08:01:05|_|Device1|_|8|_|Int/0.101|_| C|_|09-03-2011_06:49:52|_|Device3|_|192.165.161.41^150^router|_|192.165.161.41^150^router P|_|04-04-2012_12:37:14|_|Device4_|CPU|_|CPU| File2 contents are : C|_|09-03-2011_06:49:52|_|Device5|_|cpu-1|_|cpu-1| P|_|04-04-2012_12:37:14|_|Device4|_|CPU|_|CPU C|_|09-03-2011_06:49:52|_|Device3|_|192.165.161.41^150^router|_|192.165.161.41^150^router I have some conditions which I need to chcek and then filter desired o/p , those conditions are as below :

if(($array1[0]=~/c/i) && ($array2[0]= ~/c/i)) { if ($arra1[2]=array2[2] && array1[4]=array2[4]) { don't add anything } else { append matched line to file2 } } elsif(($array1[0]=~/M/i) && ($array2[0]= ~/M/i)) { if ($arra1[2]=array2[2] && array1[4]=array2[4]) { don't add anything } else { append matched line to file2 } }
I tried to apply this logic in below code but not working as expected , please help !

#!/usr/local/bin/perl use strict; my @array1; my @array2; #my $count=0; my $i=0; open (OUTFILE ,">file2.dat") || die "can't create" ; open (FILE1,"file1.dat"); while(my $value1=<FILE1>) { my $i= $i +1 ; @array1=split('\|_\|',$value1); chomp($value1); my $dev1=$array1[2] ; my $attribute1=$array1[4]; my $profile1=$array1[0]; open (FILE2, "file2.dat"); while (my $value2=<FILE2>) { @array2=split('\|_\|',$value2); chomp($value2); my $dev2=$array2[2] ; my $attribute2=$array2[4]; my $profile2=$array2[0]; if(($profile1 = ~/C/i) and ($profile2 = ~/C/i)) { if(($dev1 = $dev2) && ($attribute1 = $attribute2)) { $i ++ ; } else { print OUTFILE "$value1" ; } } else { if(($profile1 = ~/M/i) and ($profile2 = ~/M/i)) { if(($dev1 = $dev2) && ($attribute1 = $attribute2)) { $i ++ ; } else { print OUTFILE "$value1" ; } } } } } close FILE1; close FILE2; close OUTFILE; }

In reply to Re^2: Compare two arrays by perl555
in thread Compare two arrays by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.