in Education by
I am trying to compare two variables in which are usually strings. These variables are generated from a database, $var1 from one db and $var2 from another. When I compare them in a loop I use the ne operator. However there are times when I these variables are null or undef. The comparison is done as follows: foreach my $var1 (@$varlist) { if ($var1 ne $var2) { print "vars are not equal"; } } The issue is that if $var1 or $var2 are undef then I get an error. However, I need to be able to compare then values as undef b/c I will have to write them. I considered converting the variables to a string 'NULL' and then back but that seemed inefficient. Any way to fix this? Thanks! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Check if they are defined, too: foreach my $var1 (@$varlist) if ( ! defined $var1 || ! defined $var2 || $var1 ne $var2 ) print "vars are not equal"; This prints that they're not equal if both are undefined. If you want another behaviour, just change the if expression.

Related questions

0 votes
    Is there a way to compare two structure variables?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    Which of the following can potentially change the dtype of a series? (a) reindex_like (b) index_like (c ... and answers pdf, Data Science interview questions for beginners...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    A Web site that allows users to enter text, such as a comment or a name, and then stores ... Application Security in chapter Database Programming Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I have two collections for example CollectionA and CollectionB both have common filed which is hostname Collection A ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    In one Google sheet workbook, lets say I have Sheet1 with 5 rows as Sheet1 And I have Sheet2 as ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    Lets say I have this following table: +------+-------------------------+-- ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Which of these method of class String is used to compare two String objects for their equality? (a) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    How will you compare two lists in Python?...
asked Nov 26, 2020 in Technology by JackTerrance
0 votes
    For instance, given ( 1, 2, 5, 6, 7), i'd like to determine that 3 and 4 are missing? ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    For instance, given ( 1, 2, 5, 6, 7), i'd like to determine that 3 and 4 are missing? ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    How do I use Perl to create graphs? I'm running scheduled job that creates text reports. I'd like ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    How do I use Perl to create graphs? I'm running scheduled job that creates text reports. I'd like ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    How do I use Perl to create graphs? I'm running scheduled job that creates text reports. I'd like ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
...