2011-03-16 10 views
1

3つの関数があります。どのようにそれらを1つにマージするのですか? 私は関数3にテーブル名のリストを持っています。関数2はそのテーブルに対応するノード名を持ち、関数1は各テーブルの列数を持っています。 私は、3つの入力値すべてで1つの関数を優先し、関数入力によって渡される手がかりに応じて必要なものだけを使用します。3つの関数を1つにマージする

function_1() 
{ 
while read tbl col_num 
do 
    mysql test -e"call mysql.createtable('$tbl', $col_num);"  
done << mytbl_list 
test.authadv 45 
test.fee 29 
test.finadv 54 
mytbl_list 
} 

# match tables names with the respective nodes 

function_2() 
{ 
while read tbl tag 
do 
    php -e xmlread_new.php $filename $tbl $tag 
done << heredc 
test.authadv AUTHADV 
test.fee FEE 
test.finadv FINADV 
heredc 
} 


# export data to excel. Change the table names as required 

function_3() 
{ 
while read tbl_name 
do 
    mysql --table -e"select * from $tbl_name" > $report_name.txt 
done << tbl_heredoc 
test.authadv 
test.fee 
test.finadv 
tbl_heredoc 
} 
+0

、私は一度だけ書き込むことが可能であるならば3回「test.authadv」を書きたくはありません:) – shantanuo

+0

誰か、誰が「doesnのことを前と**後**例を教えてくださいあなたの仕事は終日分かります;-) – shellter

答えて

0

テーブル名と属性を格納する配列を使用します。これらの配列にテーブル名などを読み込んで格納するための4番目の関数を作成します。次に、他の3つの関数がそれらの配列にアクセスできます。

つまり
read_tables() 
{ 
    counter=0 
    while read -r tbl col_num tag 
    do 
     tables[counter]=$tbl 
     col_nums[counter]=$col_num 
     tags[counter]=$tag 
     ((counter++)) 
    done << 'TABLES' 
     test.authadv 45 AUTHADV 
     test.fee  29 FEE 
     test.finadv 54 FINADV 
TABLES 
} 

create_tables() 
{ 
    for ((i=0; i<counter; i++)) 
    do 
     mysql test -e"call mysql.createtable('${tables[i]}', ${col_nums[i]});"  
    done 
} 

# match tables names with the respective nodes 

match_tags() 
{ 
    for ((i=0; i<counter; i++)) 
    do 
     php -e xmlread_new.php "$filename" "${tables[i]}" "${tags[i]}" 
    done 
} 


# export data to excel. Change the table names as required 

export_excel() 
{ 
    for ((i=0; i<counter; i++)) 
    do 
     mysql --table -e"select * from ${tables[i]}" > "$report_name.txt" 
    done 
} 
0

どのように機能が互いに依存しているかわかりません。しかし、1つの大きな関数ではなく、より多くの関数と小さな関数を持つ方がよいスタイルになっているはずです。

キーワード:懸念の分離、1つのジョブに対する1つのツール。

機能が小さい場合は、その理由を簡単に判断できます。それらをテストする方が簡単です。それらの1つを再利用する場所を見つける方が簡単です。

+0

機能は互いに依存しません。入力はすべての関数で共通で、3つの異なる場所でテーブル名を変更する必要はありません:) – shantanuo

+0

function3はfunction2または〜1を呼び出さず、逆も同様です。これらの関数を呼び出す他の関数(文)はありません。どのテーブル名を変更したくないのですか?$ tbl? –

関連する問題