Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Array Reference (Again)

by perlcapt (Pilgrim)
on Oct 14, 2004 at 13:22 UTC ( #399198=note: print w/ replies, xml ) Need Help??


in reply to Array Reference (Again)

The understanding of passing references is REALLY important for OO-Perl. I have spent much time playing with all the possible syntaxes of referencing and de-referencing. I'm sure there are always more things to learn about the subject. But I suggest you all discover all you can.
Play with referenced multi-depth structures as much as possible. For example:

#!/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


The result should look like this:

ref to HASH HASH(0x1d5f518) ARRAY(0x1d5f548) ref to HASH ARRAY(0x22526c) ARRAY(0x1d5f488) ref to ARRAY word1 word2 word3


Comment on Re: Array Reference (Again)
Select or Download Code
Re^2: Array Reference (Again)
by jonnybe (Scribe) on Oct 14, 2004 at 16:21 UTC
    ++perlcapt...short and sweet. There is one misleading aspect to the code, however:

    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.. }

    You don't list all reference types; missing are: REF, CODE, and GLOB.

    Maybe I'm just picking nits :)

    Jon

Re^2: Array Reference (Again)
by TheEnigma (Pilgrim) on Oct 14, 2004 at 17:09 UTC
    ++perlcapt from me also.
    Read your bio - Welcome abord cap'n!
    BTW, what a great name for a boat: Mother of Perl!

    TheEnigma

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (14)
As of 2014-08-21 15:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (136 votes), past polls