2012-05-04 39 views
2

我是Perl新手,需要帶12行8列的製表符分隔文件,對數據執行一些計算,然後將其導出到具有相同格式的新文件行和列。我假設我需要將文件導入到一個數組,但有什麼特別的導入?我不確定是否需要導入到多個數組或多維數組,或者只是爲所有數據導入一個普通數組。那麼導出數據有什麼特別的地方,它的格式是一樣的嗎?我迷路了!希望這是有道理的! 以下是我迄今所做的:包含行和列的Perl製表符分隔文件

#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>; 
+1

你所要求的perl的一般教程。互聯網上有很多。如果您遇到麻煩,請回答具體問題! – Nick

+0

對不起,我不需要解釋整個事情,我只是想知道是否有什麼特別的關於以這種特定的列/行格式導入文件?我真的環顧四周,但沒有人談論使用列和行。 – user1375907

+1

你需要看看'split'。在你的情況'split/\ t /'。據推測,你的行沒有什麼特別之處,因爲它們只是文件中的行? – Borodin

回答

0

你可以嘗試使用Text::CSV_XS模塊。該文檔有非常好的例子。使用此功能的優點是,對於包含選項卡的值,您不會遇到問題,而如果使用split,則需要檢查選項卡是否位於雙引號之間,例如, "foo\tbar"

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; 
+0

非常感謝!這非常有幫助! – user1375907

+0

對不起,我不確定第一行是...? (我很綠)這是我放在一起。我要在早上測試它,但只是想我會發布它: – user1375907

+0

另外,戴夫,你會對(付費)在線輔導感興趣嗎? – user1375907

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; 
+0

這看起來非常像我的解決方案。但以各種方式改變,所以它不會再工作:-) –

相關問題