如何创建在Perl中二维数组? [英] How can I create a two-dimensional array in Perl?

查看:206
本文介绍了如何创建在Perl中二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在试图通过48传递一个矩阵文件是32到Perl中的多维数组。我能够访问所有的值,但我访问一个特定的值有问题。任何帮助将大大AP preciated。我已经把一个链接到数据集,以及在code我有以下。

下面是对数据集的链接:
http://paste-it.net/public/x1d5301/

下面是我为code现在。

 #!的/ usr / bin中/ perl的打开文件,testset.txt或死亡$ !;
我@lines =<文件&gt ;;我的$大小=标@lines;我的@matrix =(1 .. 32);
我的$ I = 0;
我的$ J = 0;
我@micro;的foreach($矩阵)
{的foreach($线)
{
    推@ {$微[$矩阵]},$线;
}
}


解决方案

它似乎没有你明白, $矩阵只表示 @matrix 时,它后面紧跟着一个数组索引: [$槽] 。否则, $矩阵 @matrix 一个完全不同的变量(两者从%不同矩阵以及)。请参见 perldata

 #!的/ usr / bin中/ perl的
使用英语;

的使用英语! - 这样

这带来了 $ MATCH $preMATCH $ POSTMATCH 和incurrs可怕的 $&安培; ,$`, $ 罚款。你应该等到你的使用的英语变量,然后只导入。

 打开文件,testset.txt或死亡$ !;

两件事情:1)使用词汇的文件句柄以及2)使用三ARG 打开

 我@lines =<文件&gt ;;

只要我在挑选:不思乐普大文件。 (不是这里的情况,但它是一个很好的警示。)

 我的$大小=标@lines;我的@matrix =(1 .. 32);
我的$ I = 0;
我的$ J = 0;
我@micro;

我看到我们在利润!!这里阶段...

 的foreach($矩阵){

您没有一个变量 $矩阵你有一个变量 @matrix

 的foreach($线){

同样的事情是真实的与 $行

 推@ {$微[$矩阵]},$线;
    }
}

重写:

 使用严格的;
使用警告;
使用英语QW< $ OS_ERROR取代; #$!
打开(我的$输入,'<','testset.txt)或死亡$ OS_ERROR;#我要去承担空格分隔的,因为你不显示
我@matrix;
#而(定义($ _ =< $输入>))...
而(小于$输入>){
    终日啃食; #脱光记录分隔符
    #与基准加载@matrix的每个时隙来填充阵列
    #用空格分割线。
    推@matrix,[分体式] #拆拆=('',$ _)
}

I am currently trying to pass a matrix file that is 32 by 48 to a multi-dimensional array in Perl. I am able to access all of the values but am having issues accessing a specific value. Any help would be greatly appreciated. I have put a link to the data set as well as the code I have below.

Here is a link to the data set: http://paste-it.net/public/x1d5301/

Here is what I have for code right now.

#!/usr/bin/perl

open FILE, "testset.txt" or die $!;
my @lines = <FILE>;

my $size = scalar @lines;

my @matrix = (1 .. 32);
my $i = 0;
my $j = 0;
my @micro;

foreach ($matrix)
{

foreach ($lines)
{
    push @{ $micro[$matrix]}, $lines;
}
}

解决方案

It doesn't seem you understand that $matrix only indicates @matrix when it is immediately followed by an array indexer: [ $slot ]. Otherwise, $matrix is a completely different variable from @matrix (and both different from %matrix as well). See perldata.

#!/usr/bin/perl
use English;

Don't! use English--that way!

This brings in $MATCH, $PREMATCH, and $POSTMATCH and incurrs the dreaded $&, $`, $' penalty. You should wait until you're using an English variable and then just import that.

open FILE, "testset.txt" or die $!;

Two things: 1) use lexical file handles, and 2) use the three-arg open.

my @lines = <FILE>;

As long as I'm picking: Don't slurp big files. (Not the case here, but it's a good warning.)

my $size = scalar @lines;

my @matrix = (1 .. 32);
my $i = 0;
my $j = 0;
my @micro;

I see we're at the "PROFIT!!" stage here...

foreach ($matrix) {

You don't have a variable $matrix you have a variable @matrix.

    foreach ($lines) {

Same thing is true with $lines.

        push @{ $micro[$matrix]}, $lines;
    }
}

Rewrite:

use strict;
use warnings;
use English qw<$OS_ERROR>; # $!
open( my $input, '<', 'testset.txt' ) or die $OS_ERROR;

# I'm going to assume space-delimited, since you don't show
my @matrix;
# while ( defined( $_ = <$input> ))...
while ( <$input> ) {
    chomp; # strip off the record separator
    # load each slot of @matrix with a reference to an array filled with
    # the line split by spaces.
    push @matrix, [ split ]; # split = split( ' ', $_ )
}

这篇关于如何创建在Perl中二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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