annel has asked for the wisdom of the Perl Monks concerning the following question:
Actually I'm not sure how to defined my question based on specific title, I will try to explain more here then.
Text 1: Entry 1 1 N 2 D EOE
Text 2: Entry 3 6 N 7 EOE Entry 5 4 D 9 D EOE
Text 1 has 1 entry while text 2 has 2 entries. What I wish to do is to compare the contents of entry by entry between files and so on. For example above,both files have first entry then it will proceed to comparison. But, Text 1 does not contain second entry as Text 2 then output a warning message" Text 1 has missing content".
use strict; use warnings; my (@data1,@data2); my (@array,@array2); open my $fh, '<', 'Text1' or die "Could not open file to read:$!"; while (<$fh>) { if (/^\Entry (\w+)/../\EOE/){ push @array, $_; if (/^(\Entry)/){ push @data1 ,$1; } } } open $fh, '<', 'Text2' or die "Could not open file to read:$!"; while (<$fh>) { if (/^\Entry (\w+)/../\EOE/){ push @array2, $_; if (/^(\Entry)/){ push @data2 , $1; } } } print @array2; my $size = @data1 < @data2 ? @data1 : @data2; my $result= @data1 < @data2 ? "File 1 has missing data" : "File 2 has +missing data"; my $ori = @data1 == @data2 ? 1:0; my $i; for( $i = 0; $i < $size; $i++){ if ($data1[$i] ne $data2[$i]){ printf "%s is mismatch with %s\n",$data1[$i],$data2[$i]; } else { &compare(); } } print "$result\n" if (!$ori);
I do not include the subroutine here. I would like to ask if any idea to store content of each entry specifically to ease the comparison work? I have tried array but it stores content of all entries as it has. Instead access to the content of entry 3, it access to all content of text 2. Is hash able to do so? Any suggestion is much appreciated. I am not expecting any codes but some suggestion on which functions or method would really be a big help. I am trying to write Perl myself. Cheers.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Methods to store content
by Laurent_R (Canon) on Dec 24, 2013 at 10:30 UTC | |
|
Re: Comparison between data range
by Anonymous Monk on Dec 24, 2013 at 08:14 UTC | |
by annel (Novice) on Dec 24, 2013 at 08:24 UTC | |
by Anonymous Monk on Dec 24, 2013 at 08:39 UTC | |
by annel (Novice) on Dec 24, 2013 at 09:07 UTC | |
|
Re: Methods to store content (choosing a data structure for comparing entries from two different files , hash)
by Anonymous Monk on Dec 24, 2013 at 10:19 UTC |