Home: Perl Programming Help: Beginner:
Modification of non-creatable array value attempted



yleberre
New User

Sep 29, 2015, 9:23 AM


Views: 1790
Modification of non-creatable array value attempted

Hello,

I run this piece of code :


Code
     1	@lines = split (/\n/, $read_buffer, -1); 
2 $lines[0] = $residu . $lines[0] if (defined $residu);
3 if ($read_buffer =~ /\n$/) {
4 undef $residu;
5 } else {
6 $residu = $lines[$#lines];
7 $lines[$#lines] = '';
8 }


This code is heavily used among many identical processes on several hosts (same hardware, same OS : SunOS 5.10 x86, same version of perl : 5.8.4 : default Solaris package) and, on a specific host, from time to time (rarely), one process aborts with the error 'Modification of non-creatable array value attempted' on line 7 with the value of index -1.

What have i missed ?
How $#lines may be modified between line 6 and line 7 ?

Thanks for your advices.
Y. Le Berre


BillKSmith
Veteran

Sep 29, 2015, 6:52 PM


Views: 1782
Re: [yleberre] Modification of non-creatable array value attempted

The following code duplicates the problem. You can see that it happens when the array is not defined.

Code
use strict; 
use warnings;
my @lines;
my $residu = $lines[$#lines];
$lines[$#lines] = '';

Output:

Code
Modification of non-creatable array value attempted, subscript -1 at C:\Users\Bi 
ll\perl\guru\yleberre.pl line 5.

Good Luck,
Bill


yleberre
New User

Sep 30, 2015, 11:33 AM


Views: 1777
Re: [BillKSmith] Modification of non-creatable array value attempted

I thought line 1 of initial script :


Code
@lines = split (/\n/, $read_buffer, -1);


was sufficient to populate systematically @lines array but split function documentation indicates :


Quote
split /PATTERN/,EXPR,LIMIT
...
Note that splitting an EXPR that evaluates to the empty string always produces zero fields, regardless of the LIMIT specified.
...


So, I must deal with empty $read_buffer before assigning @lines.

Thanks.
Y. Le Berre


FishMonger
Veteran / Moderator

Oct 1, 2015, 8:56 AM


Views: 1749
Re: [yleberre] Modification of non-creatable array value attempted

Please post more of your code to give us more context to see why you chose to populate the array in this way and if you can, post sample input data.