CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
$1 not containing real string from brackets

 



roniz5
New User

Feb 18, 2007, 12:25 AM

Post #1 of 2 (1819 views)
$1 not containing real string from brackets Can't Post

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 tryed and noticed that this works:

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

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



So, I tryed to use just a simple compare as the above 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


davorg
Thaumaturge / Moderator

Feb 22, 2007, 5:05 AM

Post #2 of 2 (1790 views)
Re: [roniz5] $1 not containing real string from brackets [In reply to] Can't Post

Why isn't my octal data interpreted correctly?


Code
use strict; 
use warnings;

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

if ($val2 =~ /the value (0x\w+)/) {
if ($val1 == hex($1)) {
print "OK";
} else {
print "$1 is not $val1\n";
}
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives