In Search of Elegant Code - Change only the first null element in an array



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?

.



Relevant Pages

  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)
  • Re: passing a string to a dll
    ... Joe, I really appreciate you taking the time to demonstrate this. ... sure how I would implement indexing it for random alphanumeric codes. ... I might handle the array. ... I actually have been wondering if I could use a second string ...
    (microsoft.public.vc.mfc)
  • Re: passing a string to a dll
    ... I might handle the array. ... I actually have been wondering if I could use a second string ... look at insertion cost, organization cost, and search cost. ...
    (microsoft.public.vc.mfc)
  • Re: Array Type Mismatch
    ... Dim resultAs String ... I am still somewhat confused as to why nothing is stored in the array. ... Public Function TextBoxGetLine(....arguments... ...
    (microsoft.public.access.formscoding)
  • Re: NumberFormatException:please help me......!!!
    ... Record that has string and array of double as members) and file could ... follows it on a line of the input file, you could use the String as a key to ... contain String representations of doubles. ...
    (comp.lang.java.programmer)