Connecting Tech Pros Worldwide Help | Site Map

String comparison

Member
 
Join Date: Jul 2008
Posts: 70
#1: Jul 15 '09
I have a string with some special characters as below:

Expand|Select|Wrap|Line Numbers
  1. chr1:52055616-52061682
  2.  
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's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Jul 15 '09

re: String comparison


Quote:

Originally Posted by lilly07 View Post

I have a string with some special characters as below:

Expand|Select|Wrap|Line Numbers
  1. chr1:52055616-52061682
  2.  
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.

Expand|Select|Wrap|Line Numbers
  1. $str1 = 'chr1:52055616-52061682';
  2. $str2 = 'chr1:52055616-52061682';
  3. if ($str1 eq $str2) {
  4.    print "They are equal";
  5. }   
Member
 
Join Date: Jul 2008
Posts: 70
#3: Jul 15 '09

re: String comparison


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:

Expand|Select|Wrap|Line Numbers
  1. $hashfile = $ARGV[0];
  2. open (LIST1,$hashfile) || die "FILE not found\n";
  3. while(<LIST1>) {
  4.   ($var1,$var2) = split (/\t/,$_);
  5.   $hash{$var1} = $var2
  6. }
  7. close(LIST1);
  8.  
  9. $datafile = $ARGV[1];
  10.  open (LIST2,$datafile) || die "FILE not found\n";
  11. while(<LIST1>) {
  12. chomp $_;
  13.   my @v = split((/\t/,$_);
  14.       while(($k,$v) = each %hash) {
  15.        if($v eq $v[0]) {
  16.           $ID = $k;
  17.         }
  18.        }
  19.  print "$ID\t$_\n";
  20. }
  21.  
But getting the key from hash using $v[0] never works. Please lte me know whether my approach has any problem.
Expand|Select|Wrap|Line Numbers
  1. if($v eq $v[0]) {
  2.           $ID = $k;
  3.         }
  4.  
  5.  
Thanks and Regards
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Jul 15 '09

re: String comparison


Try chomping LIST1 before putting it into a hash:

Expand|Select|Wrap|Line Numbers
  1.  while(<LIST1>) {
  2.    chomp;
  3.    ($var1,$var2) = split (/\t/,$_);
  4.    $hash{$var1} = $var2
  5.  }
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#5: Jul 15 '09

re: String comparison


and instead of a while loop use a foreach loop:

Expand|Select|Wrap|Line Numbers
  1.        foreach my $key (keys %hash) {
  2.         if($hash{$key} eq $v[0]) {
  3.            $ID = $key;
  4.            last;#<--get out as soon as possible
  5.          }
  6.      }
  7.  
Member
 
Join Date: Jul 2008
Posts: 70
#6: Jul 20 '09

re: String comparison


Thanks Kelvin. I also tried using the above steps.
Member
 
Join Date: Jun 2009
Posts: 54
#7: Jul 21 '09

re: String comparison


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.
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#8: Jul 28 '09

re: String comparison


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:
Expand|Select|Wrap|Line Numbers
  1. while (<LIST2>) {
  2.  
and NOT
Expand|Select|Wrap|Line Numbers
  1. while (<LIST1>) {
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. ## for LIST1
  2. $var2 =~ s/^\s*//;      $var2 =~ s/\s*$//;
  3. $hash{$var1} = $var2 ;
  4.  
  5.  
  6. ###for LIST2
  7.  my @v = split((/\s*\t\s*/,$_); 
  8.  
  9.  
Reply