Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Accessing 2D array values and comparing

by ig (Vicar)
on Jul 05, 2012 at 07:18 UTC ( #979972=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://979972]
and John Coltrane plays...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2018-05-26 16:03 GMT
Find Nodes?
    Voting Booth?