Perl制表符分隔文件,包含行和列

时间:2012-05-04 21:05:31

标签: perl

我是Perl的新手,需要使用带有12行和8列的制表符分隔文件,对数据执行一些计算,然后将其导出到具有相同行和列格式的新文件。我假设我需要将文件导入数组但是导入有什么特别之处吗?我不确定是否需要导入多个数组或多维数组,或者只需要导入所有数据的普通数组。那么导出数据是否有任何特殊之处,以便它采用相同的格式?我迷路了!希望这是有道理的! 以下是我到目前为止所做的事情:

#Pseudocode:
#Ask user for the path to the input file
#Ask user for the dilution factor
#Ask user for the path to the output file
#Read in the file with columns and rows
#Put all data into 1 array

#For each index of above array:
#Multiply given OD readings by 50ug/ul 
#Multiply all readings by dilution factor from user 
#Print converted values into the tab-delimited output folder with 12 columns * 8 rows

#Add header to output file to describe what was done
##################################################

#This program converts absorbance data from a file into DNA concentration based on a     standard     conversion
#factor of 50ug/ul and a dilution factor given by a user.
#It then prints the converted values into an output folder of the same dimensions of the             original and adds a header.

#!/usr/bin/perl -w

print "Please enter the pathway to the file containing the UV spec data: \n";
$input_file = <STDIN>;

chomp $input_file;

    unless (open(INPUTFILE, $input_file))
{
    print "Cannot open file \"$input_file\"\n\n";
    exit;
}
@input_data = <INPUTFILE>;

close INPUTFILE;

$input_data = join('', @input_data);    #puts data into a single string for easier     processing???

$input_data =~ s/\s//g;         #remove any whitespace

print "Please enter the dilution factor for these samples: \n";
$dilution_factor = <STDIN>;
chomp $dilution_factor;

foreach my $calc_data (@input_data)
{
    my $new_conc = $input_data * 50 * $dilution_factor;
    return $new_conc;
}


print "Please enter the pathway for the file you you want to save the converted data in:     \n";
$output_file = <STDIN>;

3 个答案:

答案 0 :(得分:2)

为了简化程序,我稍微改变了程序的界面。假设这是在一个名为dilute.pl的文件中,你可以这样称呼它(稀释系数为0.1):

  $ ./dilute.pl 0.1 < input.dat > output.dat

代码如下所示:

  #!/usr/bin/perl

  use strict;
  use warnings;

  # Get the dilution factor from the command line
  my $dilute = shift;

  # Get the input (all at once)
  my $input = do { local $/; <> };

  # Change the values.
  # This assumes that your input is all floating point numbers.
  # You might need to tweak the regex if that's not the case.

  $input =~ s/(\d+\.\d+)/$1 * 50 * $dilute/eg;

  # Write the output
  print $input;

答案 1 :(得分:0)

您可以尝试使用Text::CSV_XS模块。文档有很好的例子。使用此功能的好处是,对于包含选项卡的值不会有问题,而如果使用split,则需要检查选项卡是否在双引号之间,例如"foo\tbar"

答案 2 :(得分:0)

#!/usr/bin/perl -w

$header = 'This file gives the concentration of the DNA based on OD value, dilution, and     concentration.';

print "Please enter the pathway to the file containing the UV spec data: \n";
$input_file = <STDIN>;

chomp $input_file;

unless (open(INPUTFILE, $input_file))
{
    print "Cannot open file \"$input_file\"\n\n";
    exit;
}
close INPUTFILE;

# Get the dilution factor from the command line
my $dilute = shift;

# Get the input (all at once)
my $input_data = do { local $/; <> };

# Change the values.

$input_data =~ s/(\d+\.\d+)/$1 * 50 * $dilute/eg;

# Write the output to the output file

$output_file = "calc_values";
unless (open(CALCVALUES, ">$output_file"))
{
    print "Cannot open file \"$output_file\"\n\n";
    exit;
}

format CALCVALUES =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  <<<<<<<<<<<<<<<<<<<<<<<<...
$header

^<<<<<<<<<<<~~
$output_file
.

write;
exit;