G'day johnfl68,

"Arrays, hashes, array of hashes, hash of arrays, etc., they have always been confusing for me. ... What am I missing?"

While you may be having some difficulty with certain data structures, I suspect your underlying problem is in the areas of data types, their references and the commands to use to manage them. This is particularly borne out by comments about getting HASH(0xd6a9690) instead of a string and, in a separate reply, "Can't use string ... as a HASH ref".

If you haven't already read perlintro, I suggest you do. If you have read it, I'd recommend revising the "Perl variable types" section. Don't be put off by terms such as "introductory" and "beginner"; this particular piece of documentation is peppered with links to more detailed information and advanced topics: it's an excellent stepping-stone when researching some specific area and you're not sure where to start. The section I pointed out has multiple links: for your immediate needs, perhaps start with perlreftut and perldata.

I put together the following script, which uses a data structure similar to yours, but which has a whole range of data types and references. It shows how each can be examined: is it defined? is it a reference? what sort of reference? does it have content? how to dereference? The first half of the examples are likely to be things you're dealing with generally; the second half are somewhat more advanced (perhaps come back to them at a later date).

#!/usr/bin/env perl -l use strict; use warnings; use IO::Handle; my $complex_data_hash_ref = { not_used_1 => '', example => { undefined => undef, empty_string => '', non_empty_string => 'string', empty_array_ref => [], non_empty_array_ref => [ 1, 2, 3 ], empty_hash_ref => {}, non_empty_hash_ref => { x => 24, y => 25, z => 26 }, scalar_ref => \42, glob_ref => \*STDOUT, code_ref => sub { print "\n\t\tHello, world!\n" }, ref_ref => \\99, blessed_ref => IO::Handle::->new(), ref_ref_ref => \\\999, regex => qr{match}, vstring => v65, vstring_ref => \v65, }, not_used_2 => '', }; my @example_keys_to_parse = qw{ undefined empty_string non_empty_string empty_array_ref non_empty_array_ref empty_hash_ref non_empty_hash_ref glob_ref code_ref ref_ref blessed_ref ref_ref_ref regex vstring vstring_ref }; for my $example_key (@example_keys_to_parse) { my $example_key_value = $complex_data_hash_ref->{example}{$example_key}; examine_data($example_key, $example_key_value); } sub examine_data { my ($in_key, $in_value) = @_; print "Examining '$in_key':"; if (ref $in_value) { print "\tThe value of '$in_key' is a reference."; print "\tThe value of '$in_key' is '$in_value'."; my $ref = ref $in_value; print "\tThe ref() value of '$in_key' is '$ref'."; if ($ref eq 'SCALAR') { my $deref = ${$in_value}; print "\tIts dereferenced value is '$deref'."; } elsif ($ref eq 'ARRAY') { my @deref = @{$in_value}; if (@deref) { print "\tIts dereferenced value has these elements: ", "'@deref'."; } else { print "\tIts dereferenced value has no elements."; } } elsif ($ref eq 'HASH') { my %deref = %{$in_value}; my @deref_keys = keys %deref; if (@deref_keys) { print "\tIts dereferenced value has these keys: ", "'@deref_keys'."; my @deref_values = values %deref; print "\tIts dereferenced value has these values: ", "'@deref_values'."; } else { print "\tIts dereferenced value has no key-value pairs +."; } } elsif ($ref eq 'GLOB') { print $in_value "\tA globref can be a filehandle opened fo +r writing."; } elsif ($ref eq 'CODE') { $in_value->(); } elsif ($ref eq 'REF') { examine_data('dereferenced__' . $in_key, ${$in_value}); } else { print "\tTODO: Further work on this type ", 'is left as an exercise for the reader.'; } } else { print "\tThe value of '$in_key' is not a reference."; if (defined $in_value) { print "\tThe value of '$in_key' is defined."; if (length $in_value) { print "\tThe value of '$in_key' is '$in_value'."; } else { print "\tThe value of '$in_key' is zero-length."; } } else { print "\tThe value of '$in_key' is not defined."; } } }

Notes:

The output is rather lengthy. It's in the spoiler, below. You may find it's a useful exercise to determine what you think the output of each example is before looking.

— Ken


In reply to Re: If hash not defined help by kcott
in thread If hash not defined help by johnfl68

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.