首页 > 解决方案 > perl - 将数组放入一个数组中

问题描述

我想把一个变量放在一个数组中,然后用一个变量取这个数组$splited_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";

我想要输出值 print "$splited_line[0]\n"; 中的一行而不是一个单词。

标签: arraysperl

解决方案


好的,我认为您在这里的目标是一次取一行,更新行,然后将其放入数组中。该数组应包含所有重新格式化的行,以便您以后可以访问它们。

首先,您要包含所有修改行的数组是@splited_line. 这就是您尝试在程序末尾打印第一行的原因:

print "$splited_line[0]\n";

您的问题之一是您@splited_line使用my. 这意味着它的值是循环本地的,你不能从循环外部访问它,比如你的程序结束。(这里有一些解释变量范围的东西,如果你需要的话。)

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

接下来,我更改了循环,以便在构造修改后的行时,将其添加到该数组的末尾。每次循环时,您都在处理下一行输入,并将一行添加到@splited_line. 我习惯push将新行添加到数组的末尾:

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

差不多就是这样。这里的代码只是稍微调整了一下:

#!/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";

您会看到我添加了另一行来打印第二个已处理的行:

print "$splited_line[1]\n";

当我运行程序时,输出如下所示:

file loaded 
2018-11-29 d k 12
2018-11-30 d k 23

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


推荐阅读