in reply to Re^2: printing sub return values to a file
in thread printing sub return values to a file

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

Replies are listed 'Best First'.
Re^4: printing sub return values to a file
by ikegami (Patriarch) on Nov 19, 2009 at 21:29 UTC

    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>; }
Re^4: printing sub return values to a file
by lomSpace (Scribe) on Nov 19, 2009 at 21:23 UTC
    Toolic!
    Thanks!
    LomSpace