http://www.perlmonks.org?node_id=979972


in reply to Accessing 2D array values and comparing

I too am uncertain about your objective, but from what I see I suspect your solution is more complicated than it needs to be. The following is much simpler, and might be what you want. If not, it might be a basis for modification to get what you want. To that end, I suggest starting with something simple that works, and then making one change at a time to make it closer to what you want, making sure your program runs as expected at each step.

Anyway, here is something you might consider:

use strict; use warnings; use Data::Dumper; my $minimum_difference = 10; my $input_filename = 'e_d.txt'; open(my $input_fh, '<', $input_filename) or die "$input_filename: $!"; my $previous_fields; while (my $line = <$input_fh>) { chomp($line); my $fields = [ split(/\t/, $line) ]; if( defined($previous_fields) and $previous_fields->[0] eq $fields->[0] and $previous_fields->[1] ne $fields->[1] and $previous_fields->[-2] - $fields->[-2] < $minimum_difference ) { print "Failed test:\n" . Dumper([ $previous_fields, $fields ]) + . "\n\n"; } $previous_fields = $fields; }

I have made many assumptions. Most significant is the assumption that you are only interested in differences between consecutive lines in the input file.

I have used Data::Dumper to dump both arrays of fields when the test fails. This module is very helpful when you are developing code that deals with data structures. You also might want to read perldsc.