1

I want to put a the value of $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] in splited_line array, but I don't know how.

my script is :

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

}

I want a result as $splited_line[2], thanks for any information.

update

I do this :

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];
}

output:

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.

I don't know why

7
  • $split_line is not an array, it's a scalar. Do you mean @split_line? Commented Nov 30, 2018 at 8:47
  • yes, i mean @split_line Commented Nov 30, 2018 at 8:51
  • my @splited_line = @temp_arr[0 .. 12]; Note the my: Please turn on strict and warnings. Commented Nov 30, 2018 at 9:08
  • Your split pattern seems to be incorrect. my @temp_array = split(' ', $line) or = split(/\s+/, $line) Commented Nov 30, 2018 at 9:15
  • If you need to re-order temp_array fields then use an array slice, e.g. my @interesting_array = @temp_array[3,4,5,10,11], i.e. @interesting_array gets fields 4, 5, 6, 11 and 12 from $line Commented Nov 30, 2018 at 9:17

1 Answer 1

2

Your code looks rather strange.

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

That doesn't look like it should all be on the same line.

#use strict; use warnings;

Commenting those out is a very bad idea!

foreach my $line(@lines) {

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

    push(@temp_arr);

I'm not sure what you think you're doing here? push() takes (at least) two arguments - an array (which you have) and some items to add to the array (which you've omitted). Without a second argument, push() does nothing.

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

This seems like a very long way to write 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";

Ok, so this is where you're going wrong. You are putting all of the elements of @temp_arr into a string and then assigning that (single) string to @splited_line. This leaves @splited_line with one element - containing your string. What you probably want is just @splited_line = @temp_arr.

    push(@splited_line);

Another pointless push(). Why are you adding those to your code?

    print $splited_line[2];

As @splited_line only contains one element, $splited_line[2] is going to contain undef.

}

I think you want something like this:

#!/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]; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

One more nitpick; don't use POSIX; because it will load hundreds of symbols into your package, either use POSIX (); or import the specific things you need from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.