【问题标题】:perl - put array in a arryperl - 将数组放入一个数组中
【发布时间】:2018-11-30 13:46:08
【问题描述】:

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

【问题讨论】:

标签: arrays perl


【解决方案1】:

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

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

print "$splited_line[0]\n";

您的问题之一是您使用my 在循环内声明数组@splited_line。这意味着它的值是循环本地的,你不能从循环外部访问它,比如你的程序结束。 (Here's something 解释变量范围,如果你需要的话。)

所以我要添加一个新行来声明该循环之外的@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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多