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, $_; } }
In reply to Re: Methods to store content
by Laurent_R
in thread Methods to store content
by annel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |