package TSV; use strict; # ************************ # *** TSV Manuipuation *** # ************************ # Given a fresh file, grab the next row sub getRow { my ($file) = @_; my $line; if($line = <$file>) { chomp $line; $line =~ tr/\n\r//d; $line =~ s/\t+$//; my (@row) = split(/\t/, $line); return @row; } else { die "Row not found!"; } } # Given a file and our headers, grab the next line and split it into # name->value pairs sub getRowHash { my ($file, @header) = @_; my $line; if($line = <$file>) { $line =~ tr/\n\r//d; $line =~ s/\t+$//; my %row; my (@entries) = split(/\t/, $line); for(my $i = 0; $i < $#header + 1; $i++) { $row{$header[$i]} = $entries[$i]; } return (%row); } else { print "X\n"; # We didn't find a line, so we'll return nothingness return undef; } } sub hashToRow { my ($header, $hash) = @_; my (@row) = map { $hash->{$_} } @$header; return @row; } sub printRow { my ($file, @data) = @_; print $file (join "\t", @data) . "\n"; } 1;