0

I want put a variable in a array and take this array by a variable $splited_line[0]

My script is:

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

I want a line not a word in the output value print "$splited_line[0]\n";.

jack94
  • 41
  • 1
  • 7

1 Answers1

1

OK, I think what you're aiming to do here is taking the lines one at a time, update the line, and then put it into an array. This array should contain all the reformatted lines so than you can access them later.

First, the array which you're wanting to contain all the modified lines is @splited_line. This is why you're trying to print the first line at the end of your program:

print "$splited_line[0]\n";

One of your problems here is that you're declaring the array @splited_line inside the loop using my. This means that its value is local to the loop, and you can't access it from outside the loop, such as the end of your program. (Here's something to explain variable scope, if you need it.)

So I'm going to add a new line to declare the scope of @splited_line outside that loop. This means that the loop can add items to the array, and you can see those items from outside the loop.

Next, I've changed the loop so that when it constructs the modified line, it adds that to the end of that array. Each time you go round the loop, you're processing the next line of input, and adding a line to @splited_line. I've used push to add the new line to the end of the array:

push @splited_line, $temp_splited_line;

You've not told us anything about your input file file.log, so I've made one up which looks like this:

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

And that's pretty much it. Here's the code only very slightly adjusted:

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

You'll see that I've added another line to print the second processed line:

print "$splited_line[1]\n";

When I run the program, the output looks like this:

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

There's still quite a few things which could be changed, but this should get you back on track.

Tim
  • 9,171
  • 33
  • 51