Hi monks, I have a problem, I have written a sub-routine that looks for pairs of things in an array. However, I want to do this for 5 arrays and so repreat the code for all five (couldn't think of an efficient way to do it). The problem is that when I call the sub-routine, as below only the @pairs and @pairs5 values are printed. If i comment out the printing of @pairs, the values of @pairs2 are printed etc. I was wondering if anyone new whether this is because I am returning more than one value from the sub-routine?? And also if anyone could suggest a more efficient way of writing the code? Thanks, x
my @pairs = get_pairs (@segment1); my @pairs2 = get_pairs (@segment2); my @pairs3 = get_pairs (@segment3); my @pairs4 = get_pairs (@segment4); my @pairs5 = get_pairs (@segment5); print "my pairs are: @pairs"; print "my pairs2 are: @pairs2"; print "my pairs3 are: @pairs3"; print "my pairs4 are: @pairs4"; print "my pairs5 are: @pairs5"; sub get_pairs { (@segment1, @segment2, @segment3, @segment4, @segment5) = @_ +; my (@pairs, @pairs2, @pairs3, @pairs4, @pairs5); my $i; my $nn_pair; # find and print all nn pairs from the sequence. foreach $i (1..$#segment1) { push @pairs, "$segment1[$i-1]$segment1[$i]\n"; } return @pairs; foreach my $k (1..$#segment2) { push @pairs2, "$segment2[$k-1]$segment2[$k]\n"; } return @pairs2; foreach my $j (1..$#segment3) { push @pairs3, "$segment3[$j-1]$segment3[$j]\n"; } return @pairs3; foreach my $h (1..$#segment4) { push @pairs4, "$segment4[$h-1]$segment4[$h]\n"; } return @pairs4; foreach my $m (1..$#segment5) { push @pairs5, "$segment5[$m-1]$segment5[$m]\n"; } return ( @pairs5); }

In reply to subroutines + returning more than 1 value by Anonymous Monk

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.