In Search of Elegant Code - Change only the first null element in an array
- From: usenet@xxxxxxxxxxxxxxx
- Date: 5 Oct 2005 13:55:17 -0700
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?
.
- Follow-Ups:
- Re: In Search of Elegant Code - Change only the first null element in an array
- From: Bob Walton
- Re: In Search of Elegant Code - Change only the first null element in an array
- From: attn.steven.kuo@xxxxxxxxx
- Re: In Search of Elegant Code - Change only the first null element in an array
- From: John Bokma
- Re: In Search of Elegant Code - Change only the first null element in an array
- From: it_says_BALLS_on_your forehead
- Re: In Search of Elegant Code - Change only the first null element in an array
- Prev by Date: Re: optimize log parsing
- Next by Date: Re: Need File I/O Help....
- Previous by thread: 3 arrays into @_
- Next by thread: Re: In Search of Elegant Code - Change only the first null element in an array
- Index(es):
Relevant Pages
|