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

Hi, Under Window NT environment Here is my script, I am comparing two txt files which are pipe delimited line by line if they match then their is no output but if they do not match then it gives an output about the difference. My both files are loaded in the array and I am comparing line by line. How can I compare a value in a certain field within an array line by line between two arrays so that I can have the result I want.Instead of comparing whole line I want to compare one field from both arrays and out put result The txt files are attached for your visual ease and they are pipe delimited If you can help me out thanks
my($snapshot, $baseline); # Defining txt files # First file $snapshot = "./302-snap.txt"; # List of Svcs currently running loc at c:\perl # Second file $baseline = "./302-base.txt"; # List of All Svcs which should be running my(@arr1, @arr2, @result); # Defining arrays my($fld1, $fld2, $fld3, $fld4); # Defining variables for fields my($match, $cnt, $val1, $val2, $finalresult); # Defining scalar variables $match = "N"; #Open snapshot file and insert every line into @arr1 open(SNAPSHOT, $snapshot) or die "Unable to open $snapshot."; # Open t +he txt file and place it in filehandle $cnt = 0; while ( <SNAPSHOT> ) # Looping thru the filehandle snapshot { $arr1[$cnt] = $_; $cnt = $cnt + 1; } close (SNAPSHOT); # close the filehandle snapshot #Open baseline file and insert every line into @arr2 open(BASELINE, $baseline) or die "Unable to open $baseline."; # Open t +he txt file and place it in filehandle $cnt = 0; while ( <BASELINE> ) # Looping thru the filehandle baseline { $arr2[$cnt] = $_; $cnt = $cnt + 1; } close (BASELINE); # close filehandle baseline # Outer loop is for baseline file # Inner loop is for snapshot file # Taking one element from @arr2 (baseline) and comparing it with all t +he elements in @arr1(snapshot) and # If their is no match then insert that name into @result. $cnt = 0; foreach $val2 (@arr2) # referring to baseline { foreach $val1 (@arr1) # referring to snapshot { if ($val1 eq $val2) { $match = "Y"; } } if ($match eq "N") { $result[$cnt] = $val2; $cnt = $cnt + 1; } $match = "N"; } $finalresult = 0; # initializing to zero foreach $val1 (@result) { $finalresult = $finalresult + 1; } if ($finalresult > 0 ) { print "\nList of SICK Siebel-Components \n\n"; # If some svc is not functioning foreach $val1 (@result) { ($fld1,$fld2,$fld3,$fld4) = split(/\|/,"$val1");# separating req fields for output print "Svc-Component $fld4, of Siebel-Svc '$fld3'($fld2)\n"; } } else { print "\nAll Siebel-Components are Functioning Fine\n"; # When all sv +cs are running fine }
I want to compare the matching between field 5 for each line for both files contents of Testbase.txt
S2_NXLKPRD|SMECacheMgr|Analysis Cache Manager|SMECacheMgr|Enabled|Runn +ing|08/24/2001 11:00:49|20|0|1| S2_NXLKPRD|SMEProxyMgr|Analysis Proxy Manager|SMEProxyMgr|Enabled|Runn +ing|08/24/2001 11:00:49|20|0|1|
contents of snapbase.txt
S2_NXLKPRD|SMECacheMgr|Analysis Cache Manager|SMECacheMgr|error|Runnin +g|08/24/2001 11:00:49|20|0|1| S2_NXLKPRD|SMEProxyMgr|Analysis Proxy Manager|SMEProxyMgr|Enabled|Runn +ing|08/24/2001 11:00:49|20|0|1|

Edit Masem 2001-08-28 - Code tags

Replies are listed 'Best First'.
Re: comparison
by chromatic (Archbishop) on Aug 29, 2001 at 00:59 UTC
    Use split.
    my $field = 5; # the field to compare open(FIRST, "> $file1") or die "Can't open $file1: $!"; open(SECOND, "> $file2") or die "Can't open $file2: $!'; while (defined(my $first = <FIRST>) { defined(my $second = <SECOND>) or last; my @first = split(/\|/, $first); my @second = split(/\|/, $second); if ($first[$field] eq $second[$field]) { # mark match } else { # mark no match } }
    It's possible to shorten that somewhat by list slicing split, but this'll do for now. Be aware that the first element of a list has an index of 0.