Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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 !

Replies are listed 'Best First'.
Re: Weird hash access problem
by toolic (Bishop) on May 26, 2011 at 18:31 UTC
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 (Abbot) 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 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!

Re: Weird hash access problem
by jfroebe (Parson) 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

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 contemplating the Monastery: (4)
As of 2016-07-23 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (221 votes). Check out past polls.