Hi,
it seems to me you basically need to read file 1 and store the contents into a hash in which the heading ("Entry 1" or simply 1 if that's sufficient) is the key and the content of the entry the value; depending on what exactly you want to do afterwards, the content of the entry may be stored as a scalar string or as an array reference or even a hash reference. Then, you read file 2 and, for each entry, you check if that entry is in the hash (i.e. was in file 1) and, if it exists, you can just proceed with the comparison you need to do between the content of the entry with what was stored in the hash. A simplified version of what you need might look like this:
# open your file 1 as $IN1...
my %hash_file_1;
while (<$IN1>) {
if (/entry) {
my $key = $_;
my $value = "";
while (<$IN1>) {
last if /EOE/;
$value .= $_;
}
$hash_file_1{$key} = $value;
}
}
# open file 2 as $IN2
while (<$IN2>) {
if (/entry) {
my $key = $_;
if (exists $hash_file_1{$key}) {
my $value2 = "";
while (<$IN2>) {
last if /EOE/;
$value2 .= $_;
}
# do the comparison between $value2 and $hash_file_1{$ke
+y}
# and print what you need
}
}
}
Update: 1. I had not seen Anonymous Monk's last post when I wrote this, I might not have written this if I had seen it, although the approaches are quite different. 2. the inner while loop is duplicated code and could go in a function with something like this.
# open your file 1 as $IN1...
my %hash_file_1;
while (<$IN1>) {
if (/entry) {
my $key = $_;
my $value = read_entry($IN1);
$hash_file_1{$key} = $value;
}
}
# open file 2 as $IN2
while (<$IN2>) {
if (/entry) {
my $key = $_;
if (exists $hash_file_1{$key}) {
my $value2 = read_entry($IN2);
# do the comparison between $value2 and $hash_file_1{$ke
+y}
# and print what you need
}
}
}
sub read_entry {
my $FH = shift;
my $value;
while (<$FH>) {
return $value if /EOE/;
$value .= $_;
}
}
The advantage is that if you want to change the data structure from a simple hash to, for example, a hash of arrays, the change needs to be in only one place instead of two, in the sub, which would have to return an array ref instead of a string.
sub read_entry {
my $FH = shift;
my @value_arr;
while (<$FH>) {
return \@value_arr if /EOE/;
chomp; # might not be needed, will depend on what will be do
+ne next with the array of lines
push @value_arr, $_;
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.