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;}

In reply to finding neighbors of an element in arrays by persianswallow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.