Hi, please find the attached code and its containing the samples outpu +ts. please let me know your comments. use strict; use warnings; ## variable declaration my ( $i, $j, $k); my ( @MergeArray,@NumArray, @SortArray, %ResultHash ); ## assigning values to variables $i = $j = $k = 0; @MergeArray = @NumArray = @SortArray = %ResultHash =(); ## given the Inputs my @a1 = ( 100, 204, 312 ); my @a2 = ( 102, 313, 409 ); my @a3 = ( 205, 206, 315 ); my @a4 = ( 207, 210, 314 ); ########################## PROGRAM START ######################## ## merging all arrays with names @MergeArray = ( 'a1', @a1,'a2', @a2,'a3', @a3,'a4', @a4 ); ## grepping numbers only @NumArray = grep!/^a/, @MergeArray; ## sorting numbers @SortArray = sort @NumArray; ## reading sorted numbers for( $i = 0; $i < @SortArray - 1; $i++ ){ my $no1 = $SortArray[$i]; ## one number compare with all other numbes for( $j = $i+1; $j < @SortArray; $j++){ my $no2 = $SortArray[$j]; my $diff = $no2 - $no1; ## getting contiguous differnces function ContiguousFun( $diff, $no1, $no2 ); } } ## printing results print "Contiguous sequences and the numbers appropriate array name and + position of the index\n"; foreach my $res (sort keys %ResultHash ){ ## you have to change the condition whatever you need next if($res < 2 or $res > 9); print "\nnumber $res\n"; print $ResultHash{$res}; } ########################## PROGRAM END ######################## ## sub function for 'Contiguous' sub ContiguousFun{ my $diff_no = shift; my $arrno1 = shift; my $arrno2 = shift; my $arrno1_info = GettingNameAndIndexVal( $arrno1 ); my $arrno2_info = GettingNameAndIndexVal( $arrno2 ); ## checking if diff_no exists or not if(exists $ResultHash{$diff_no}){ $ResultHash{$diff_no} = "$ResultHash{$diff_no}"."\n$arrno1 && +$arrno2 ==> $arrno1_info AND $arrno2_info"; }else{ $ResultHash{$diff_no} = "$arrno1 & $arrno2 ==> $arrno1_info AN +D $arrno2_info"; } } ## getting array names and index value function sub GettingNameAndIndexVal{ my $arrno = shift; my $arrayname =" "; foreach my $item ( @MergeArray ){ ## getting array name $arrayname = $item if( $item =~ /a/i ); ## initialized to index value if array name found $k = -1 if( $item =~ /a/i); ## processing numbers only not a array names ie, ingoring arra +y names next if( $item =~ /a/i); $k++; ## returning array name with index value return "$arrayname, $k" if( $item == $arrno ); } } __END__ sample inputs my @a1 = ( 100, 204, 312 ); my @a2 = ( 102, 313, 409 ); my @a3 = ( 205, 206, 315 ); my @a4 = ( 207, 210, 314 ); Sample Results Contiguous sequences and the numbers appropriate array name and positi +on of the index number 2 100 & 102 ==> a1, 0 AND a2, 0 204 && 206 ==> a1, 1 AND a3, 1 205 && 207 ==> a3, 0 AND a4, 0 312 && 314 ==> a1, 2 AND a4, 2 313 && 315 ==> a2, 1 AND a3, 2 number 3 204 & 207 ==> a1, 1 AND a4, 0 207 && 210 ==> a4, 0 AND a4, 1 312 && 315 ==> a1, 2 AND a3, 2 number 4 206 & 210 ==> a3, 1 AND a4, 1 number 5 205 & 210 ==> a3, 0 AND a4, 1 number 6 204 & 210 ==> a1, 1 AND a4, 1

In reply to Re: Searching parallel arrays. by Anonymous Monk
in thread Searching parallel arrays. by BrowserUk

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.