469,306 Members | 1,557 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Comparing Hex Numbers in Strings.

5
Hi,

I'm trying to sort a file according to a hexa value that appears in every line.

Since I don't remember how to convert hex2int, I tried and noticed that this works:

$val1= "0x0A"; $val2=10;

if ($val1 == $val2) {print "OK";}



So, I tried to use just a simple compare as the above (hexa against integer, with no conversions) in my script that sorts the file.

But here I encountered this problem: $1 does not get the whole meaning of the string in brackets. What do I mean? look at the next lines, they should have worked but they don't:

$val1=10; $val2="this line contains the value 0x0A";

if ($val2 =~ /the value (0x\w)/)

{if ($val1 == $1) {print "OK";}}



This did not work!!

Just to show that the problem is with the $1 interpretation, I printed out to the screen the content of $1 and it printed out correctly: 0x0A.

Can someone explain this to me?



Thanks

Roni

roniz5@yahoo.com
Feb 19 '07 #1
5 12147
KevinADC
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. my $val1 = 10;
  2. my $val2 = "this line contains the value 0x0A";
  3.  
  4. if ($val2 =~ /the value (0x\w+)/){
  5.    if ($val1 == oct($1)) {
  6.       print "OK";
  7.    }
  8. }
Feb 19 '07 #2
More precisely and therefore robust would be:
Expand|Select|Wrap|Line Numbers
  1. /the value (0x[\da-f]+)/i
Greetz, Doc
Feb 19 '07 #4
roniz5
5
Expand|Select|Wrap|Line Numbers
  1. my $val1 = 10;
  2. my $val2 = "this line contains the value 0x0A";
  3.  
  4. if ($val2 =~ /the value (0x\w+)/){
  5.    if ($val1 == oct($1)) {
  6.       print "OK";
  7.    }
  8. }
--------------------------------------------------

Thanks a lot, it works!
And thank you also for the link to the relevant info.

Roni
Feb 19 '07 #5
roniz5
5
More precisely and therefore robust would be:
Expand|Select|Wrap|Line Numbers
  1. /the value (0x[\da-f]+)/i
Greetz, Doc
Thanks you very much, I will use the /i - you saved me a future bug...

Roni
Feb 19 '07 #6

Post your reply

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

Similar topics

4 posts views Thread by agent349 | last post: by
3 posts views Thread by steve | last post: by
7 posts views Thread by Tweaxor | last post: by
3 posts views Thread by esparkman | last post: by
19 posts views Thread by nitro_punk85 | last post: by
2 posts views Thread by franzdawg3 | last post: by
2 posts views Thread by cobraun | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.