Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Hash Printing reveals Hash Memory Location

by perlStuck (Sexton)
on Feb 03, 2012 at 00:54 UTC ( #951570=perlquestion: print w/ replies, xml ) Need Help??
perlStuck has asked for the wisdom of the Perl Monks concerning the following question:

Hey there! Currently, I'm having troubles with Perl printing the memory location of a hash when I feel that I haven't given it sufficient cause to do so. This is a word processing tool that I'm making for myself to help in the study of foreign languages. I'm reading in English words and their foreign (in my case, German) equivalent, and storing them in a hash. When one iterates over the hash, however, Perl decides that I'm telling it to print out the hashes memory location as well as all the elements of the hash I have purposefully put in there. It's interesting to note that the hashes memory location does not receive a 'tag' from the hash_processor subroutine. Any help would be greatly appreciated.

sub add_words::main () { my %words = {}; my $english; my $foreign; until ('1' == '0') { print "Enter English: "; chomp($english = <STDIN>); print "Enter German: "; chomp($foreign = <STDIN>); if (($english && $foreign) eq '-1') { last; } ($english, $foreign) = &hash_processor::function($english, $foreign +, '0'); $words{$english} = $foreign; } foreach my $word (sort keys %words) { # First output is description of hash's mem location, and I DON'T K +NOW WHY. print "$word ==> $words{$word}\n"; } } sub hash_processor::function () { my ($english, $foreign, $process) = @_; if ($process == '0') { $english = "$english" . "$hash_processor::tag_count"; $foreign = "$foreign" . "$hash_processor::tag_count"; } $hash_processor::tag_count++; return ($english, $foreign); }

Output is something like:

HASH(0x950c5e6) ==> word ==> Wort two words ==> zwei Woerter etc. ==> usw.

Comment on Hash Printing reveals Hash Memory Location
Select or Download Code
Re: Hash Printing reveals Hash Memory Location
by tobyink (Abbot) on Feb 03, 2012 at 01:04 UTC

    You have:

    my %words = {};

    You want:

    my %words = ();

    Or better yet:

    my %words;

    Essentially in that first line, you're adding an entry to the hash where the key is a reference to an anonymous second hash, and the value is undefined. Because you can't use references as hash keys, the reference is stringified (which looks like a memory location).

    In other words, my %words = {}; is basically a shorthand for:

    my %words; my $ref_to_anon_hash = {}; $words{ $ref_to_anon_hash } = undef;

      Ah. Thank you!

Re: Hash Printing reveals Hash Memory Location
by kejohm (Hermit) on Feb 03, 2012 at 01:55 UTC

    In addition to tobyink's post above, I would suggest using strict and warnings in your code. This will ensure that Perl will warn you about any dubious constructs in your code. For instance, the line my %words = {}; would have resulted in the message Odd number of elements in hash assignment. These messages can be quite useful and often point you in the right direction if your code isn't behaving as expected. The perldiag manpage can be used to look up the meanings of the messages and the diagnostics pragma can be used to print out the more verbose messages from perldiag.

Re: Hash Printing reveals Hash Memory Location
by jwkrahn (Monsignor) on Feb 03, 2012 at 03:51 UTC
    if (($english && $foreign) eq '-1') {

    The expression $english && $foreign will evaluate to either true (1) or false ("") but will never be equal to '-1'.



    sub hash_processor::function () { my ($english, $foreign, $process) = @_;

    Your prototype says that the subroutine accepts no arguments but the next line says that your subroutine does accept three arguments.    You shouldn't use prototypes.

    You are using package names but I don't see where you are creating a package namespace.



    if ($process == '0') {

    That is usually written as $process == 0 or $process eq '0' depending on whether $process contains a string or a number.



    $english = "$english" . "$hash_processor::tag_count"; $foreign = "$foreign" . "$hash_processor::tag_count";

    That is usually written as:

    $english .= $hash_processor::tag_count; $foreign .= $hash_processor::tag_count;

      The expression $english && $foreign will evaluate to either true (1) or false ("") but will never be equal to '-1'.

      Wrong. The "&&" operator doesn't return 1 or "". It returns the expression on the right side of "&&" if and only if both expressions are true.

      use 5.010; my $english = 'foobar'; my $foreign = -1; say ($english && $foreign); # says "-1" say ($foreign && $english); # says "foobar"
        It returns the expression on the right side of "&&" if and only if both expressions are true.
        It returns the expression on the right side if the expression on the left side is true.
        $ perl -E 'say "Yes" if "Foo" && 0 eq 0' # says "Yes" $ perl -E 'say "Yes" if "Foo" && "" eq ""' # says "Yes" $ perl -E 'say "Yes" if "Foo" && 0 eq ""' # Nothing

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-09-01 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls