in reply to how to push multiples row of values into hash and do comparison

Your use of 'strict' is an excellent practice. However, declaring all your variables at the start of your file defeats much of the advantage. You should declare each variable in the smallest possible scope. (Strict will usually tell you if place a declaration in to small a scope or if you try to use a variable that is out of scope.) This style makes it nearly impossible to accidentally use data left over from a previous iteration or from elsewhere in your program. When you are debugging a problem with a variable, you can be certain that the error occurs in the block where it is declared.

Consider an example from your code. It appears that you are storing stale values of $x1, $x2, etc. in your hash. Even if it is not a problem, you must disprove it. If those variables were declared inside the loop, the question would never arise.

Bill
  • Comment on Re: how to push multiples row of values into hash and do comparison

Replies are listed 'Best First'.
Re^2: how to push multiples row of values into hash and do comparison
by darkmoon (Novice) on Oct 22, 2018 at 07:41 UTC

    Hi, thanks for the reply. There was an error if i declare the variable inside the while loop. So i was thinking that maybe i can declare it as global variable so that I still can get the output after the while loop.

      Before addressing your comment, let me clear up one point of confusion. In perl, the term 'global variable' means the same thing as 'package variable' (declared with 'our' or 'use vars'). You did not declare any of these. By moving the declaration, you increased the scope of a lexical variable to include the entire file.

      when you see a message that a variable is not declared, it can mean any of several things.

      • You forgot to declare the variable.
      • You misspelled the variable
      • You used the variable some place you did not intend.
      • You declared the variable in too narrow a scope.
      Removing 'strict' will suppress the message, but not fix any of the errors. Moving the declaration to the start of the file will probably also suppress the message. It will 'fix' the scope problem, but none of the others. In exchange, you have given up this protection against errors you may make in the future.

      The solution is 'smallest possible scope'. Move the declaration not to the start of the file, but to the start of the smallest block which includes every necessary use of the variable.

      Bill