Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:56:34+00:00 2026-06-06T09:56:34+00:00

I am trying to compare two variables in which are usually strings. These variables

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:56:37+00:00Added an answer on June 6, 2026 at 9:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to compare two strings. One stored in a file, the other retrieved
I'm trying to compare two variables of type nullable(of boolean) in VB.NET 2010. One
I am trying to compare two tools execution time, which I have installed in
I am trying to compare two strings in a simple shell script. I was
I am trying to compare two strings in Smalltalk, but I seem to be
I am trying to compare two dates. I have this code which I thought
I am trying to compare two NSDates one is created by the viewDidLoad method
Currently I am trying to compare two variables, newhtml and oldhtml . I get
I am trying to compare the addresses of two variables in memory: chunk_t *old_chunk,
I am trying to compare two entries of 6 numbers, each number which can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.