Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays perl-将数组放入arry中_Arrays_Perl - Fatal编程技术网

Arrays perl-将数组放入arry中

Arrays perl-将数组放入arry中,arrays,perl,Arrays,Perl,我想将一个变量放入一个数组,并通过一个变量获取这个数组$splited\u line[0] 我的剧本是: #!/usr/bin/perl use DBI; use Data::Dumper; use DBD::mysql; use POSIX; #use strict; use warnings; #"titi.log" or die $!; open(FILE,"<file.log"); print "file loaded \n"; my @lines=<FILE&g

我想将一个变量放入一个数组,并通过一个变量获取这个数组
$splited\u line[0]

我的剧本是:

#!/usr/bin/perl

use DBI;
use Data::Dumper;
use DBD::mysql;
use POSIX;

#use strict; use warnings;

#"titi.log" or die $!;

open(FILE,"<file.log");

print "file loaded \n";

my @lines=<FILE>;

#tout les valeurs du fichier se trouve dans le tableau lines

close(FILE);

my @temp_arr;


foreach my $line(@lines)
{
    @temp_arr=split('\s',$line);
    #converti en nombre
    $temp_arr[12] =~ s/[^\d.]//g;
    #converti en entier
    $temp_arr[12] = floor($temp_arr[12]);

    #enlève les simple cote
    $temp_arr[10] =~ s/^'|'$//g;

    if ($temp_arr[12] > 5)
    {
        if ($temp_arr[1] eq "Jan")
        {
            $temp_arr[1] = "01"
        }
        if ($temp_arr[1] eq "Feb")
        {
            $temp_arr[1] = "02"
        }
        if ($temp_arr[1] eq "Mar")
        {
            $temp_arr[1] = "03"
        }
        if ($temp_arr[1] eq "Apr")
        {
            $temp_arr[1] = "04"
        }
        if ($temp_arr[1] eq "May")
        {
            $temp_arr[1] = "05"
        }
        if ($temp_arr[1] eq "Jun")
        {
            $temp_arr[1] = "06"
        }
        if ($temp_arr[1] eq "Jul")
        {
            $temp_arr[1] = "07"
        }
        if ($temp_arr[1] eq "Aug")
        {
            $temp_arr[1] = "08"
        }
        if ($temp_arr[1] eq "Sep")
        {
            $temp_arr[1] = "09"
        }
        if ($temp_arr[1] eq "Oct")
        {
            $temp_arr[1] = "10"
        }
        if ($temp_arr[1] eq "Nov")
        {
            $temp_arr[1] = "11"
        }
        if ($temp_arr[1] eq "Dec")
        {
            $temp_arr[1] = "12"
        }

        $temp_splited_line = "$temp_arr[4]-$temp_arr[1]-$temp_arr[2] $temp_arr[3] $temp_arr[10] $temp_arr[12]";
        my @splited_line = $temp_splited_line;

        #my @temp_array = split(' ', $line)

        $temp_arr[3] $temp_arr[10] $temp_arr[12];

        #2017-07-13 21:34:30 SG_PICK_BOL 5.428
        #$slow_trans_line = "$word5-$word2-$word3 $word4 $word11 $word13";
        #print "$slow_trans_line\n";
    }
}

print "$splited_line[0]\n";
#/usr/bin/perl
使用DBI;
使用数据::转储程序;
使用DBD::mysql;
使用POSIX;
#严格使用;使用警告;
#“titi.log”或die$!;

打开(文件,“好的,我想你的目标是一次一行,更新行,然后将其放入一个数组中。这个数组应该包含所有重新格式化的行,以便你以后可以访问它们

首先,要包含所有修改行的数组是
@splited\u line
。这就是为什么要在程序末尾打印第一行的原因:

print "$splited_line[0]\n";
这里的一个问题是,您正在使用
my
在循环内声明数组
@splited_line
。这意味着它的值是循环的本地值,您无法从循环外访问它,例如程序的结尾。(如果需要,请解释变量范围。)

因此,我将添加一个新行来声明循环外部的
@splited_line
范围。这意味着循环可以向数组中添加项,并且您可以从循环外部看到这些项

接下来,我更改了循环,这样当它构造修改后的行时,它会将该行添加到该数组的末尾。每次循环时,您都会处理下一行输入,并将一行添加到
@splited_line
。我使用
推送
将新行添加到数组的末尾:

push @splited_line, $temp_splited_line;
您没有告诉我们有关您的输入文件
file.log
的任何信息,因此我制作了一个如下所示的文件:

Thu Nov 29 d 2018 f g h i j k l 12.345
Fri Nov 30 d 2018 f g h i j k l 23.456
file loaded 
2018-11-29 d k 12
2018-11-30 d k 23
差不多就是这样。这里的代码只是稍微调整了一下:

#!/usr/bin/perl

use DBI;
use Data::Dumper;
#use DBD::mysql;
use POSIX;

use strict; use warnings;

#"titi.log" or die $!;

open(FILE,"<file.log");

print "file loaded \n";

my @lines=<FILE>;

#tout les valeurs du fichier se trouve dans le tableau lines

close(FILE);

my @temp_arr;
my @splited_line;


foreach my $line(@lines)
{
    @temp_arr=split('\s',$line);
    #converti en nombre
    $temp_arr[12] =~ s/[^\d.]//g;
    #converti en entier
    $temp_arr[12] = floor($temp_arr[12]);

    #enlève les simple cote
    $temp_arr[10] =~ s/^'|'$//g;

    if ($temp_arr[12] > 5)
    {
        if ($temp_arr[1] eq "Jan") { $temp_arr[1] = "01" }
        if ($temp_arr[1] eq "Feb") { $temp_arr[1] = "02" }
        if ($temp_arr[1] eq "Mar") { $temp_arr[1] = "03" }
        if ($temp_arr[1] eq "Apr") { $temp_arr[1] = "04" }
        if ($temp_arr[1] eq "May") { $temp_arr[1] = "05" }
        if ($temp_arr[1] eq "Jun") { $temp_arr[1] = "06" }
        if ($temp_arr[1] eq "Jul") { $temp_arr[1] = "07" }
        if ($temp_arr[1] eq "Aug") { $temp_arr[1] = "08" }
        if ($temp_arr[1] eq "Sep") { $temp_arr[1] = "09" }
        if ($temp_arr[1] eq "Oct") { $temp_arr[1] = "10" }
        if ($temp_arr[1] eq "Nov") { $temp_arr[1] = "11" }
        if ($temp_arr[1] eq "Dec") { $temp_arr[1] = "12" }

        my $temp_splited_line = "$temp_arr[4]-$temp_arr[1]-$temp_arr[2] $temp_arr[3] $temp_arr[10] $temp_arr[12]";
        push @splited_line, $temp_splited_line;

        #my @temp_array = split(' ', $line)

#       $temp_arr[3] $temp_arr[10] $temp_arr[12];

        #2017-07-13 21:34:30 SG_PICK_BOL 5.428
        #$slow_trans_line = "$word5-$word2-$word3 $word4 $word11 $word13";
        #print "$slow_trans_line\n";
    }
}

print "$splited_line[0]\n";
print "$splited_line[1]\n";
运行程序时,输出如下所示:

Thu Nov 29 d 2018 f g h i j k l 12.345
Fri Nov 30 d 2018 f g h i j k l 23.456
file loaded 
2018-11-29 d k 12
2018-11-30 d k 23

仍然有很多事情可以改变,但这应该会让你回到正轨。

请重新格式化你的答案,使其可读。之前的问题: