SPUG: A hash of hashes and arrays
mike
mike206 at gmail.com
Mon Aug 1 08:53:09 PDT 2005
%perldoc -f ref
ref EXPR
ref Returns a true value if EXPR is a reference, false
otherwise. If EXPR is not specified, "$_" will be
used. The value returned depends on the type of
thing the reference is a reference to. Builtin
types include:
SCALAR
ARRAY
HASH
CODE
REF
GLOB
LVALUE
If the referenced object has been blessed into a
package, then that package name is returned
instead. You can think of "ref" as a "typeof"
operator.
if (ref($r) eq "HASH") {
print "r is a reference to a hash.\n";
}
unless (ref($r)) {
print "r is not a reference at all.\n";
}
if (UNIVERSAL::isa($r, "HASH")) { # for subclassing
print "r is a reference to something that isa hash.\n";
}
On 8/1/05, Duane Blanchard <dblanchard at gmail.com> wrote:
> Hi all,
>
> I have a hash of hashes and arrays. I'm able to print the all the keys
> of the outer hash, and print the embedded hashes and arrays
> individually, now I want to print the entire contents of the outer
> (matrix) hash.
>
> I need something like:
>
> for each key in the outer hash
> { if (the key is a reference to a hash)
> { print the contents of the hash;
> }
> elsif (the key is a reference to an array)
> { print the contents of the array;
> }
> }
>
> I have everything in place, but don't know how to tell whether I'm
> looking at a hash or an array.
>
> Any hints?
>
> P.S. In linguistics, we call the embedded sentences "embedded
> sentences," clever, I know, and those into which they are embedded
> "matrix sentences." What is the parlance in Perl/formal languages?
>
>
> Duane Blanchard
>
> There are 10 kinds of people in the world;
> those who know binary and those who don't.
> _____________________________________________________________
> Seattle Perl Users Group Mailing List
> POST TO: spug-list at pm.org
> SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
> MEETINGS: 3rd Tuesdays, Location: Amazon.com Pac-Med
> WEB PAGE: http://seattleperl.org/
>
More information about the spug-list
mailing list