将 3 个功能合并为一个
我有3个功能。如何将它们合并为一个? 我有函数 3 中的表名称列表。函数 2 具有与该表相对应的节点名称,函数 1 具有每个表中的列数。 我更喜欢一个具有所有 3 个输入值的函数,而仅根据函数输入传递的线索来使用所需的值。
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
}
I have 3 functions. How do I merge them into one?
I have the list of table names in the function 3. The function 2 has node name that corresponds to that table and the function 1 has number of columns in each of the tables.
I will prefer one function with all 3 input values while using the required ones only depending upon the clue passed by the function input.
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用数组来存储表名称和属性。创建第四个函数来读取表名称等并将其存储到这些数组中。然后其他三个函数就可以访问这些数组。
Use arrays to store your table names and attributes. Create a fourth function to read and store your table names, etc., into those arrays. Then the other three functions can access those arrays.
我真的看不出这些功能如何相互依赖。然而,拥有更多、更小的功能而不是一个大的功能应该是一种好的风格。
关键词:关注点分离,一种工具做一项工作。
如果函数较小,则更容易推理它们。测试它们更容易。找到一个地方来重用其中一个会更容易。
I can't really see how the functions depend on each other. However, it is supposed to be good style to better have more and smaller functions instead of one big one.
Keywords: Separation of concerns, one tool for one job.
If you have smaller functions, it's easier to reason about them. It is easier to test them. It is easier to find a place to reuse one of them.