Reputation: 39
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
Upvotes: 1
Views: 94
Reputation: 69224
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];
}
Upvotes: 2