1

My list (@degree) is built from a SQL command. The NVL command in the SQL isn't working, neither are tests such as:

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 a for loop. Basically it goes through and grabs unique degrees from a table and ends up creating ("AFA", "AS", "AAS", "", "BS"). The list is not always this long, and the empty element is not always in that position 3.

Can anyone help?

I want to either test during the for loop, or after the loop completes for where this empty element is and then replace it with the word, "OTHER".

Thanks for anything -Ken

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks... ya i know its $degree[$i], i was just in a rush haha and the eq worked perfectly... I'm just learning perl on my own and i couldnt find this answer anywhere so thank you very much
6

Everything that Paul said. And, if you need an example:

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

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

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

Comments

1

If its actually a null in the database, try COALESCE

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

That's the SQL-standard way to do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.