String comparison

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilly07
    New Member
    • Jul 2008
    • 89

    String comparison

    I have a string with some special characters as below:

    Code:
    chr1:52055616-52061682
    If I have to compare two strings in the above format, do I have to use regex because normal $string1 eq $string2 does not work.

    Thanks.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Originally posted by lilly07
    I have a string with some special characters as below:

    Code:
    chr1:52055616-52061682
    If I have to compare two strings in the above format, do I have to use regex because normal $string1 eq $string2 does not work.

    Thanks.
    Show some more code or the context you are using it because string comparison will work.

    Code:
    $str1 = 'chr1:52055616-52061682';
    $str2 = 'chr1:52055616-52061682';
    if ($str1 eq $str2) {
       print "They are equal";
    }

    Comment

    • lilly07
      New Member
      • Jul 2008
      • 89

      #3
      Hi Kelvin,
      Thanks for your response.
      Yes you are right. It was not due to the string comparison problem.

      I have a set of keys and values in a hash and I am trying to search for the cooresponding key with getting value from the other file. And I am trying as below:

      Code:
      $hashfile = $ARGV[0];
      open (LIST1,$hashfile) || die "FILE not found\n";
      while(<LIST1>) {
        ($var1,$var2) = split (/\t/,$_);
        $hash{$var1} = $var2
      }
      close(LIST1);
      
      $datafile = $ARGV[1];
       open (LIST2,$datafile) || die "FILE not found\n";
      while(<LIST1>) {
      chomp $_;
        my @v = split((/\t/,$_);
            while(($k,$v) = each %hash) {
             if($v eq $v[0]) {
                $ID = $k;
              }
             }
       print "$ID\t$_\n";
      }
      But getting the key from hash using $v[0] never works. Please lte me know whether my approach has any problem.
      Code:
      if($v eq $v[0]) {
                $ID = $k;
              }
      Thanks and Regards

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Try chomping LIST1 before putting it into a hash:

        Code:
         while(<LIST1>) {
           chomp;
           ($var1,$var2) = split (/\t/,$_);
           $hash{$var1} = $var2
         }

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          and instead of a while loop use a foreach loop:

          Code:
                 foreach my $key (keys %hash) {
                  if($hash{$key} eq $v[0]) {
                     $ID = $key;
                     last;#<--get out as soon as possible
                   }
               }

          Comment

          • lilly07
            New Member
            • Jul 2008
            • 89

            #6
            Thanks Kelvin. I also tried using the above steps.

            Comment

            • RonB
              Recognized Expert Contributor
              • Jun 2009
              • 589

              #7
              Are there any duplicated values in the hash?

              If so then how do you want to determine which key to use for the $ID? If not, then it would be more efficient to reverse the hash and use 'exists' instead of the foreach loop.

              Comment

              • nithinpes
                Recognized Expert Contributor
                • Dec 2007
                • 410

                #8
                From your description, I believe you are trying to compare values in second file with the hash value read from first file. In that case, the second while loop should be:
                Code:
                while (<LIST2>) {
                and NOT
                Code:
                while (<LIST1>) {
                If that was just a typo while posting and not the actual problem you are facing, Kevin's code should work for you.
                Else, probably there may be accidentally included trailing/leading spaces for the values (apart from the tab delimitation).
                Try removing them:

                Code:
                ## for LIST1
                $var2 =~ s/^\s*//;      $var2 =~ s/\s*$//;
                $hash{$var1} = $var2 ;
                
                
                ###for LIST2
                 my @v = split((/\s*\t\s*/,$_);

                Comment

                Working...