Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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]
[choroba]: I don't even remember the beginning
[choroba]: someone told me to use perl instead of sed for something, and several years later, I'm developing a huge Tk application
[Discipulus]: choroba started in primary school to get some advantage in the future..
[choroba]: nono, I programmed in Basic at the primary school, on my Commodore 116/16

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2018-06-22 09:33 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (123 votes). Check out past polls.