Hi Moron,

Here's an interesting experiment (well, I think it's interesting, as I wasn't quite sure how it would turn out):

#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; my %hash = ( 'a' => 'b', 'c' => 'd', 'e' => [ 'F', 'G', 'H' ], 'i' => { 'J' => 'K', 'L' => 'M', }, ); as_hash(%hash); as_array(%hash); sub as_hash { my (%hash) = @_; printf "Dump of hash as a hash => %s\n", Dumper(\%hash); my $hash_i_J = $hash{'i'}{'J'}; printf "hash{i}{J} = '%s'\n", $hash_i_J; } sub as_array { my (@array) = @_; print "\nEach element of \@array:\n"; map { print "- $_\n" } @array; printf "\nDump of hash as an array => %s\n", Dumper(\@array); as_hash(@array); }

Which outputs:

Dump of hash as a hash => $VAR1 = { 'e' => [ 'F', 'G', 'H' ], 'c' => 'd', 'a' => 'b', 'i' => { 'J' => 'K', 'L' => 'M' } }; hash{i}{J} = 'K' Each element of @array: - e - ARRAY(0x804c8d4) - c - d - a - b - i - HASH(0x806024c) Dump of hash as an array => $VAR1 = [ 'e', [ 'F', 'G', 'H' ], 'c', 'd', 'a', 'b', 'i', { 'J' => 'K', 'L' => 'M' } ]; Dump of hash as a hash => $VAR1 = { 'e' => [ 'F', 'G', 'H' ], 'c' => 'd', 'a' => 'b', 'i' => { 'J' => 'K', 'L' => 'M' } }; hash{i}{J} = 'K'

I wanted to see what happened when the hash got interpreted as an array, and then back into a hash again.  No problem, you can treat it as an array, then treat that array as a hash, and it's all still there in the same original format (as witnessed both by dumping the structure with Data::Dumper, and by accessing $hash{i}{J}).

So when you say that doesn't work if the hash has to be more than one level deep, what part do you think isn't working?


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: enabling OO and non-OO access to the same module using a hash reference by liverpole
in thread enabling OO and non-OO access to the same module using a hash reference by Moron

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.