Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Issue with hash definition & possibly some other stuff

by sundialsvc4 (Abbot)
on Apr 08, 2013 at 21:39 UTC ( #1027604=note: print w/replies, xml ) Need Help??

in reply to Issue with grep & getting the right hash values out & possibly some other stuff

Echoing BrowserUK ... “the key,” in any hashref, must be “a string.” What you are using right now will be coerced by Perl into “a string,” but it won’t be a string that you can use in this case.   Therefore, you need to produce, from the arrays that you have, some kind of “single string,”   and to make sure that it’s the string that you want.   When developing code like this, I usually add a few extra statements ... creating a few extra scalar variables (my $d= ...; my $z= ...), inserting some statement (such as the suggested join() to produce the value, then perhaps initially adding print STDERR "d=$d, z=$z\n"; so that I can see what value Perl actually came up with (undoubtedly contrary to my expectations).   Once I’m satisfied with the two strings, I use them in a statement like $$haystack{$d}{$z} = \@zeroes;

The “extra” dollar-sign is functionally equivalent to $haystack->{$d}{$z} ... it’s up to you.

The backslash to the right of the equals-sign is also important:   a hashref is always a collection, indexed by “a string,” to “a scalar,” which in this case should be a reference to an array.   (Look up “references” here.)

Also, in all code, be sure to use: use strict; use warnings;.   Perl is designed to be very accepting and forgiving ... it will try to do what you ask, according to its own interpretation.   These two pragmas will tell it to alert you to mistakes that you probably didn’t realize you were making.   Your code should “run clean,” with no errors or warnings raised.

  • Comment on Re: Issue with hash definition & possibly some other stuff

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027604]
and the leaves swirl about...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2018-01-22 14:40 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.