首页 > 解决方案 > perl - 将数组的值放入数组中

问题描述

我想 $temp_arr[0] $temp_arr[1] $temp_arr[2] $temp_arr[3] $temp_arr[4] $temp_arr[5] $temp_arr[6] $temp_arr[7] $temp_arr[8] $temp_arr[9] $temp_arr[10] $temp_arr[11] $temp_arr[12] 在 splited_line 数组中放入一个值,但我不知道如何。

我的脚本是:

#!/usr/bin/perl use DBI; use Data::Dumper; use DBD::mysql; use POSIX;
#use strict; use warnings;

#"/mnt/toto/titi.log" or die $!;

open(FILE,"<titi.log"); print "file loaded \n"; my @lines=<FILE>; #tout les valeurs du fichier se trouve dans le tableau lines close(FILE);
#my @all_words; my @temp_arr;
#my @splited_line;

print "$lines[0]"; print "$lines[83000]";

foreach my $line(@lines) {

@temp_arr=split('\s',$line);

push(@temp_arr);

print "$temp_arr[0] $temp_arr[1] $temp_arr[2] $temp_arr[3] $temp_arr[4] $temp_arr[5] $temp_arr[6] $temp_arr[7] $temp_arr[8] $temp_arr[9] $temp_arr[10] $temp_arr[11] $temp_arr[12]\n";

@splited_line = "$temp_arr[0] $temp_arr[1] $temp_arr[2] $temp_arr[3] $temp_arr[4] $temp_arr[5] $temp_arr[6] $temp_arr[7] $temp_arr[8] $temp_arr[9] $temp_arr[10] $temp_arr[11] $temp_arr[12]\n"; #this line don't work

print $splited_line[2]; 

}

我想要 $splited_line[2] 的结果,感谢您提供任何信息。

更新

我这样做:

foreach my $line(@lines) {

@temp_arr=split('\s',$line);

push(@temp_arr);

#print "$temp_arr[0] $temp_arr[1] $temp_arr[2] $temp_arr[3] $temp_arr[4] $temp_arr[5] $temp_arr[6] $temp_arr[7] $temp_arr[8] $temp_arr[9] $temp_arr[10] $temp_arr[11] $temp_arr[12]\n";

@splited_line = "$temp_arr[0] $temp_arr[1] $temp_arr[2] $temp_arr[3] $temp_arr[4] $temp_arr[5] $temp_arr[6] $temp_arr[7] $temp_arr[8] $temp_arr[9] $temp_arr[10] $temp_arr[11] $temp_arr[12]\n";


push(@splited_line);

print $splited_line[2];
}

输出:

Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.
Use of uninitialized value in print at test7.pl line 35.

我不知道为什么

标签: arraysperl

解决方案


你的代码看起来很奇怪。

#!/usr/bin/perl use DBI; use Data::Dumper; use DBD::mysql; use POSIX;

看起来它不应该都在同一条线上。

#use strict; use warnings;

评论那些是一个非常糟糕的主意!

foreach my $line(@lines) {

    @temp_arr=split('\s',$line);

    push(@temp_arr);

我不确定你认为你在这里做什么?push()接受(至少)两个参数 - 一个数组(你有)和一些要添加到数组中的项目(你已经省略了)。没有第二个参数,push()什么都不做。

    #print "$temp_arr[0] $temp_arr[1] $temp_arr[2] $temp_arr[3] $temp_arr[4] $temp_arr[5] $temp_arr[6] $temp_arr[7] $temp_arr[8] $temp_arr[9] $temp_arr[10] $temp_arr[11] $temp_arr[12]\n";

这似乎是一个很长的写作方式print "@temp_arr\n":-)

    @splited_line = "$temp_arr[0] $temp_arr[1] $temp_arr[2] $temp_arr[3] $temp_arr[4] $temp_arr[5] $temp_arr[6] $temp_arr[7] $temp_arr[8] $temp_arr[9] $temp_arr[10] $temp_arr[11] $temp_arr[12]\n";

好的,这就是你出错的地方。您将 的所有元素@temp_arr放入一个字符串中,然后将该(单个)字符串分配给@splited_line. 这留下@splited_line了一个元素 - 包含您的字符串。您可能想要的只是@splited_line = @temp_arr.

    push(@splited_line);

另一个毫无意义push()。为什么要将这些添加到您的代码中?

    print $splited_line[2];

由于@splited_line仅包含一个元素,$splited_line[2]因此将包含undef.

}

我想你想要这样的东西:

#!/usr/bin/perl

# strict and warnings before all other code
use strict;
use warnings;

use DBI;
use Data::Dumper;
# You don't usually need to load DBD modules
use DBD::mysql;
use POSIX;

# 3-arg open() and lexical filehandle
# Check return from open().
open(my fh, '<', 'titi.log') or die "Cannot open file: $!\n";
print "file loaded \n";

# Do you really need to read the whole file in here?
my @lines = <$fh>; #tout les valeurs du fichier se trouve dans le tableau lines 
close($fh);

# No need to quote this values.
print $lines[0];
print $lines[83000];

foreach (@lines) {
    # By default, split() splits $_ on whitespace
    my @temp_arr = split;

    print "@temp_arr\n";

    my @splited_line = @temp_arr;

    print $splited_line[2]; 
}

推荐阅读