in reply to printing sub return values to a file

So far as I can see your subroutines do not explicitly return anything. With no return statement they will return the value of the last statement within them. That's fine for change_array, but the line my @oligo_array = get_oligo_seqs($in1); looks suspicious to me, since get_oligo_seqs does not return an array (I think it will give a single element containing an empty string).

I wonder if you are confusing the print statement with a return?

Replies are listed 'Best First'.
Re^2: printing sub return values to a file
by JadeNB (Chaplain) on Nov 19, 2009 at 16:15 UTC
    I think it will give a single element containing an empty string
    Not that it much matters, but it probably returns 1 (the return value of a successful print). The exceptions are if print fails for some reason, or if the file handled by $fh is empty—in which latter case I would have guessed that the return value would be $fh, but Data::Dumper suggests that it's ''—or, I suppose, if $fh isn't a readable filehandle, in which case something else happens.
Re^2: printing sub return values to a file
by lomSpace (Scribe) on Nov 19, 2009 at 20:48 UTC
    Hi cdarke!
    I made a change to the subroutine, but it only returns the first element.
    Any ideas on how I can get around that to return the all of the elements?
    LomSpace
      The following will loop through all lines of the input file (assuming you opened the file prior to calling the sub), populate the array, then return the array. I think your code was exiting the sub after reading only one line.
      sub get_oligo_seqs{ my ($fh) = shift; my @seqs; while(<$fh>){ my @fields = split /\t/; my $oligos = $fields[1]; push(@seqs,($oligos)); } return @seqs; }

        That clobbers the parent's $_. Fixed:

        sub get_oligo_seqs{ my ($fh) = shift; my @seqs; while(my $rec = <$fh>){ my @fields = split /\t/, $rec; my $oligos = $fields[1]; push(@seqs,($oligos)); } return @seqs; }

        This seems simpler to me:

        sub get_oligo_seqs{ my ($fh) = shift; my @seqs; while(my $rec = <$fh>){ push @seqs, ( split /\t/, $rec )[1]; } return @seqs; }

        Why stop there?

        sub get_oligo_seqs{ my ($fh) = shift; return map { ( split /\t/ )[1] } <$fh>; }
        Toolic!
        Thanks!
        LomSpace