Hi,
hopefully this helps to get your mind around your problem:
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Data::Dumper;
my %neigbours;
while (my $line = <DATA>) {
chomp $line;
my ($id_pathway, @pathnodes) = split ' ', $line;
my $path_length = @pathnodes;
if($path_length < 2) {
warn "Don't know whether paths with less than two nodes are al
+lowed. Skipping.";
next;
}
for (my $i = 0; $i < $path_length - 1; $i++) {
my $key = "$pathnodes[$i]$pathnodes[$i+1]";
if(defined $neigbours{$key}) {
if($neigbours{$key}->{'minpathlen'} >= $path_length) {
$neigbours{$key}->{'minpathlen'} = $path_length;
$neigbours{$key}->{'id_pathway'} = $id_pathway;
}
}
else {
$neigbours{$key}->{'minpathlen'} = $path_length;
$neigbours{$key}->{'id_pathway'} = $id_pathway;
}
}
}
say Dumper(\%neigbours);
__DATA__
A a b c d e f
B a b c
Allow me an annotation: Please use meaningful variable names. Your code is hard to read.
Regards
McA