lot*_*ths 3 arrays perl text file multidimensional-array
#!usr/bin/perl
@array = ();
open(myfile,"sometext.txt");
while(<myfile>)
{
chomp;
push(@array,[split(" ")]);
}
close(myfile);
print @array[0];
Run Code Online (Sandbox Code Playgroud)
它不是在这个多维数组中打印第一个数组的元素,而是输出十六进制(?)指针引用.如果有人知道我如何打印这个数组,请发布如何,将非常感谢.
Sin*_*nür 11
你应该使用strict和warnings.后者会告诉你访问第一行的方法是$array[0].现在,该值是对您推送到的匿名数组的引用@array.所以,你需要取消引用:print "@{ $array[0] }\n";
#!/usr/bin/perl
use strict; use warnings;
my @array;
my $input_file = 'sometext.txt';
open my $input, '<', $input_file
or die "Cannot open '$input_file': $!";
while(<$input>) {
chomp;
push @array, [ split ];
}
close $input;
print "@$_\n" for @array;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6808 次 |
| 最近记录: |