By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

String comparison

P: 89
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.
Jul 15 '09 #1
Share this Question
Share on Google+
7 Replies


KevinADC
Expert 2.5K+
P: 4,059
@lilly07
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. }   
Jul 15 '09 #2

P: 89
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
Jul 15 '09 #3

KevinADC
Expert 2.5K+
P: 4,059
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.  }
Jul 15 '09 #4

KevinADC
Expert 2.5K+
P: 4,059
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.  
Jul 15 '09 #5

P: 89
Thanks Kelvin. I also tried using the above steps.
Jul 20 '09 #6

Expert Mod 100+
P: 589
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.
Jul 21 '09 #7

nithinpes
Expert 100+
P: 410
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.  
Jul 28 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.