2012-05-04 5 views
2

私はPerlを初めて使い、12行8列のタブ区切りファイルを作成し、データの計算をいくつか行い、同じ形式の新しいファイルに書き出す必要があります行と列の私は配列にファイルをインポートする必要があると仮定していますが、インポートには特別なものがありますか?私は複数の配列や多次元配列、あるいはすべてのデータのための通常の配列にインポートする必要があるかどうかはわかりません。次に、データをエクスポートして同じ形式になるように特別なことはありますか?道に迷いました!これが理にかなってほしい!ここ は、私がこれまで行っているものです:あなたはText::CSV_XSモジュールを使用して試みることができる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

。ドキュメントにはとても良い例があります。これを使用する利点は、タブを含む値に問題がないことです。splitを使用する場合は、タブが二重引用符で囲まれているかどうかを確認する必要があります。 "foo\tbar"

2

私は、プログラムのインターフェースを簡単にするために少し変更しました。これを仮定すると、あなたは(0.1の希釈率で)このようにそれを呼び出すdilute.plと呼ばれるファイルにあります。

$ ./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

これは私のソリューションに非常によく似ています。しかし、さまざまな方法で変更されたので、もう動作しません:-) –

関連する問題