U
usenet
Suppose I have an array like this:
my @stuff = ("Just", "Some", "", "", "Text");
I want to replace the first (and ONLY the first) null element with some
string. If the array has no null values, don't do anything to it
(unless the array is empty, in which case add the string as an
element).
This almost works:
do {$stuff[$_] =~ s/^$/Null/ && last} for ( 0 .. @stuff-1 ) ;
but it won't affect an empty array as desired (so I need an extra line
to test and push).
I can do it REALLY ugly like this:
my $index = 0;
until ($stuff[$index] =~ s/^$/Null/ || $index >= @stuff - 1) {
$index++;
}
It works, but... Yuck. Any ideas for a more elegant approach?
my @stuff = ("Just", "Some", "", "", "Text");
I want to replace the first (and ONLY the first) null element with some
string. If the array has no null values, don't do anything to it
(unless the array is empty, in which case add the string as an
element).
This almost works:
do {$stuff[$_] =~ s/^$/Null/ && last} for ( 0 .. @stuff-1 ) ;
but it won't affect an empty array as desired (so I need an extra line
to test and push).
I can do it REALLY ugly like this:
my $index = 0;
until ($stuff[$index] =~ s/^$/Null/ || $index >= @stuff - 1) {
$index++;
}
It works, but... Yuck. Any ideas for a more elegant approach?