合并两个数组 [英] Merging two arrays

查看:110
本文介绍了合并两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由文件行组成的数组,并希望在指定的位置插入行。


这些行来自另一个正在处理的文件(一行(一次)直到达到某个字符串,此时我已经完成了该文件。


我应该读取我从文件2到另一个文件的行阵列?如何合并两个数组?


谢谢。

解决方案

要在指定的点添加/插入数组元素, ''使用拼接功能。

请参阅: perldoc -f splice


要将数组添加到数组的末尾,您将使用 push 功能。

请参阅: perldoc -f push


要在数组的开头添加元素,你将使用 unshift 函数。

见: perldoc -f unshift


< BLOCKQUOTE>谢谢。看起来拼接是答案,但引用令人困惑。我会google perl spice初学者,看看我得到了什么。


由于splice适用于两个数组,我想你建议我把第二个文件读入自己的数组,对吗?


你还没有提供足够的信息建议我把第二个文件加载到自己的数组中或逐行循环。

如果你发布你的剧本,我会看看我是否可以根据你的工作做出推荐。我可能还需要查看数据文件。


I have an array that consists of the lines of a file and wish to insert lines at a specified point.

The lines come from another file which is being processed (a line at a time) until a certain string is reached, at which point I''m finished with that file.

Should I read the lines I''m keeping from file 2 into another array? How do I merge the two arrays?

Thanks.

解决方案

To add/insert array elements at specified points, you''d use the splice function.
See: perldoc -f splice

To add elements to the end of an array, you''d use the push function.
See: perldoc -f push

To add elements to the beginning of the array, you''d use the unshift function.
See: perldoc -f unshift


Thanks. It looks like splice is the answer, but the reference is confusing. I''ll google perl spice beginner and see what I get.

Since splice works with two arrays, I think you''re suggesting I read the second file into its own array, right?


You haven''t provided enough info for me to recommend loading the second file into its own array or loop over it line-by-line.

If you post your script, I''ll see if I can make a recommendation based on what you''re doing. I may also need to see the data files as well.


这篇关于合并两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆