在 Perl 中创建数组的数组
我有一个包含两行值的文本文件(X 轴,y 轴) 。我想将每一行的值存储在一个数组中,所以我必须创建两个数组,然后从这两个数组创建一个数组的数组,并从数组中获取相应的值。
提前致谢。
示例:我的文本文件包含以下信息:
470 .023 470.5 .56 471 .8936 471.5 .452 472 .8963 472.5 .412 473 .123 473.5 .412 474 .965 474.5 .725 475 .745
我编写了这段代码
open (DATA,"text.txt");
@a=<.DATA\>;
foreach (@a)
{
@spi=split (//,$_);
** stored in arrays based on the index values**
@wave=@spi[0..4];
@abs=@spi[6..9];
@new=((@wave),(@abs));
print @new,"\n";
}
Possible Duplicate:
How can I split up a text file and store it into a 2d array using Perl?
I have a text file with two rows of values (X-axis, y-axis). I would like to store each row's values in an array, so I have to make two arrays, and then make an Array of Array from those two arrays, and fetch the corresponding values from the arrays.
Thanks in advance.
Example: My text file contains following info:
470 .023 470.5 .56 471 .8936 471.5 .452 472 .8963 472.5 .412 473 .123 473.5 .412 474 .965 474.5 .725 475 .745
I wrote this code
open (DATA,"text.txt");
@a=<.DATA\>;
foreach (@a)
{
@spi=split (//,$_);
** stored in arrays based on the index values**
@wave=@spi[0..4];
@abs=@spi[6..9];
@new=((@wave),(@abs));
print @new,"\n";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只做第一部分。
I'll just do the first part.
通读 perldsc,尤其是 AoA 部分。您的数组生成代码应该是:
数组只能包含标量,因此您需要存储引用(当然还有访问时的取消引用!)
Read through perldsc, especially the AoA section. Your array generation code should be:
Arrays can only contain scalars, so you need to store references (and of course dereference on access!)