Skip to content

Instantly share code, notes, and snippets.

@seouri
Created February 20, 2009 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save seouri/67465 to your computer and use it in GitHub Desktop.
Save seouri/67465 to your computer and use it in GitHub Desktop.
read/write perl
sub read_file {
my $file_name = shift;
print STDERR "READ $file_name ... ";
my @file;
open(FH, "< $file_name") or die("Can't read $file_name: $!\n");
while (<FH>) {
chomp;
my @t = split /\t/;
@t = map { s/^\s+//g; s/\s+$//g; $_ } @t;
push @file, \@t;
}
close FH;
print STDERR scalar(@file), " lines\n";
return \@file;
}
sub write_file {
my ($file_name, $file_ref) = @_;
print STDERR "WRITE $file_name ... ";
open(FH, "> $file_name") or die("Can't write $file_name: $!\n");
print FH join("\n", map { join("\t", @$_) } @$file_ref), "\n";
close FH;
print STDERR scalar(@$file_ref), " lines\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment