persianswallow has asked for the wisdom of the Perl Monks concerning the following question:
Hi i am trying to find number of neighbors of an element in some arrays.i have a list in a file and i want to check them one by one trough some arrays and find neighbors.
#!/usr/bin/perl -w use strict; my @domains1=('PF05.3','PF11001.3','PF00389.24','PF10417.3'); my @domains2=('PF01','PF02','PF11001.3','PF00389'); my @domains3=('PF00389.24','PF05.3','PF01','PF00389'); my %h; my $element; open(INPUTDOMAINLIST,'<domainlist') or die "cannot open 'domainlist'becuase:$!"; chomp (my @list=<INPUTDOMAINLIST>); foreach my $domain(@list){ foreach $element(@domains1){ $element=$_; if($element=~/(\Q$domain\E/)){ my $i=indexarray("$element",@domains1); if($i==0){ $h{$element}=$domains1[1]; } elsif($i!=0 && $i<scalar(@domains1)-1){ $h{$element}=$domains1[$i-1]; $h{$element}=$domains1[$i+1]; } elsif($i==scalar(@domains1)-1){ $h{$element}=$domains1[$i-1]; } }} print " $h{$element}"; sub indexarray{ my $s=shift; $_ eq $s && return @_ while $_=pop; -1;}
|
|---|