in reply to Array Reference (Again)
#!/usr/bin/perl -w use strict; sub FullStru { # creates the structures my $stru = { # an annonymous hash (ad hoc hash) Level2a => { # another ad hoc hash Level3a => ['a'..'z'], # an ad hoc list at +level 3 Level3b => [1..10] # another }, Level2b => [ qw(word1 word2 word3) ] # an ad hoc list +at level 2 }; return $stru; # notice that this is a reference to an annoymous ha +sh with sub levels } sub DownOne { my $ref = shift; # by using a reference, you get the whole enchila +da if ( ref($ref) eq 'HASH') { # return the elements as list. No real practical value other # than as an exercise.. at least as much as I can see return 'ref to HASH', values %$ref; } elsif (ref($ref) eq 'ARRAY') { # return the elements as a list return 'ref to ARRAY', @$ref; } elsif (ref($ref) eq 'SCALAR') { return 'ref to SCALAR', $$ref; } elsif ( not ref($ref)) { return 'not a ref'; } # and so on.. } my $X = FullStru(); # create the structure and return a pointer to it # first level my @results1 = DownOne($X); print join("\n",@results1),"\n"; shift(@results1); # get rid of that first element # second level my $elem; foreach $elem (@results1) { print "\n\t",join("\n\t",DownOne($elem)),"\n"; } # and so on.. you get the idea
ref to HASH HASH(0x1d5f518) ARRAY(0x1d5f548) ref to HASH ARRAY(0x22526c) ARRAY(0x1d5f488) ref to ARRAY word1 word2 word3
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Array Reference (Again)
by jonnybe (Scribe) on Oct 14, 2004 at 16:21 UTC | |
|
Re^2: Array Reference (Again)
by TheEnigma (Pilgrim) on Oct 14, 2004 at 17:09 UTC |