How do I replace empty elements in the "OTHER" array?

My list (@degree) is built from SQL command. The NVL command in SQL does not work, nor tests like:

if (@degree[$i] == "")
if (@degree[$i] == " ")
if (@degree[$i] == '')
if (@degree[$i] == -1)
if (@degree[$i] == 0)
if (@degree[$i] == ())
if (@degree[$i] == undef)

      

$ i is a counter variable in the for loop. Basically it goes through and grabs unique degrees from the table and finishes creating ("AFA", "AS", "AAS", "", "BS")

. The list is not always that long, and the empty item is not always at this position 3.

Can anyone please help?

I want to either check during the for loop or after the loop finishes where that empty element is and then replace it with the word "OTHER".

Thanks for anything Ken

+1


source to share


3 answers


First of all, the i-th element in the array is $ degree [$ i], not @degree [$ i]. Second, "==" for numerical comparisons - use "eq" for lexical comparisons. Third, tryif (defined($degree[$i]))



+8


source


Everything Paul said. And, if you want an example:



my @degree = ('AFA', 'AS', 'AAS', '', 'BS');

$_ ||= 'OTHER' for @degree;

print join ' ' => @degree;  # prints 'AFA AS AAS OTHER BS'

      

+5


source


If it is actually null in the database, try COALESCE

SELECT COALESCE(column, 'no value') AS column FROM whatever ...

      

This is the standard SQL way.

+1


source







All Articles