Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Weird hash access problem

by tehjord (Initiate)
on May 26, 2011 at 18:24 UTC ( #906870=perlquestion: print w/ replies, xml ) Need Help??
tehjord has asked for the wisdom of the Perl Monks concerning the following question:

Hi, im making some cheap chess game, but I got a very weird problem when accessing an hash using $1 following a regex match. The regex used works perfectly, but the problem is, when I try to access my hash table using the $1 var, it simply does not work. Ive tried first copying $1 to a temp var and then using that temp var to access my hash but with no success. Here is my code which should explain everything:
use strict; my %h = (a1 => 1); my $test = "a1 to a2"; if ($test =~ /([a-hA-H][1-8]\s*)to(\s*[a-hA-H][1-8])/) { print "test work\n"; } print $h{$1}; #print nothing print $h{a1}; #print 1 print $1; #print "a1"
THX FOR THE REPLIES ! works !

Comment on Weird hash access problem
Download Code
Re: Weird hash access problem
by wind (Priest) on May 26, 2011 at 18:30 UTC
    Change your regex, you're capturing the spacing:
    if ($test =~ /([a-hA-H][1-8])\s*to\s*([a-hA-H][1-8])/) {
Re: Weird hash access problem
by kennethk (Monsignor) on May 26, 2011 at 18:30 UTC
    $1 does not contain what you think it does. If you modify your code to wrap your output in a delimiter:
    use strict; my %h = (a1 => 1); my $test = "a1 to a2"; if ($test =~ /([a-hA-H][1-8]\s*)to(\s*[a-hA-H][1-8])/) { print "test work\n"; } print "x$h{$1}x\n"; #print nothing print "x$h{a1}x\n"; #print 1 print "x$1x\n"; #print "a1"
    you get:
    test work xx x1x xa1 x

    You are grabbing whitespace in addition to your square id. You'll get your expected result if you modify your regular expression to:

    /([a-hA-H][1-8])\s*to\s*([a-hA-H][1-8])/

Re: Weird hash access problem
by toolic (Chancellor) on May 26, 2011 at 18:31 UTC
Re: Weird hash access problem
by jfroebe (Vicar) on May 26, 2011 at 18:32 UTC

    The scope of "$1" with respect to the regular expression is within the if block IIRC. Who knows what "$1" is outside of if block. Btw, you forgot "use warnings;" after "use strict;"

    Jason L. Froebe

    Blog, Tech Blog

      It's being used in the same block as the one in which it's set.

        Interesting. I would have thought that the scope of what is in the if (...) would be only within the if (...) { } code block. The fact that it works doesn't mean the code should be written that way IMHO.

        Jason L. Froebe

        Blog, Tech Blog

Re: Weird hash access problem
by ikegami (Pope) on May 26, 2011 at 18:32 UTC

    print $1; #print "a1"

    No, it prints "a1 ". Your parens are misplaces in your regex pattern.

    ... Too slow. But wow, 4 replies in a span that could be as little as 61 seconds!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://906870]
Approved by kennethk
Front-paged by perlfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-12-22 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (121 votes), past polls