i want to add a newline to the lastest element of an array
i have tried this :
$array[-1] .= "\n";
but it doesn’t work and i get the following error message :
Modification of non-creatable array value attempted, subscript -1
i have tried a few other methods as well (without .=), without success.
should i try splice ?
thanks !
example of what i want :
@array = (
"one",
"two",
"n",
"last\n", # i want the newline to be added here
);
>Solution :
You can use $array[$#array] .= "\n"; to change last entry but make sure you are checking if array is not empty.
use strict;
use warnings;
use Data::Dumper;
my @array = (
"one",
"two",
"n",
"last",
);
print Dumper(\@array);
if ($#array != -1) {
$array[$#array] .= "\n";
}
print Dumper(\@array);