Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Try using ref() to determine what is stored at each dimension. Then you can make the decision about how to iterate. I believe that this subroutine will do the trick, however, I am creating this off the top of my head, so please don't be afraid to criticize.
sub recIterate{ my $ref = shift; my $funcRef = shift; #mutation function reference if(ref($ref) =~ /HASH/){ my %copy = %$ref; foreach my $item (keys %copy){ $copy{$item} = recIterate($copy{$item}, $funcRef); } %$ref = %copy; }elsif(ref($ref) =~ /ARRAY/){ my @copy; foreach my $item (@copy){ push(@copy, recIterate($item, $funcRef); } @$ref = @copy; }elsif(ref($ref) eq ''){ #i think this is how scalars #look to ref, but you might #want to look it up $$ref = &$funcRef($$ref); } }
In order to use this you must define what happens to each scalar value that is in the data structure with a subroutine that takes one scalar as a parameter and returns the new one. Just pass a reference to it when you call recIterate. Here is a usage example:
#you will need to define your own mutator function sub someFunc{ #removes tabs my $old = shift; my $new = $old; $new =~ s/\t//g; return $new; } %nDimHash; #this might be a HoAoHoAoAoAoS or whatever; %nDimHash = recIterate(\%recIterate, \&someFunc);
That should do the trick. Each dimension that is a hash or an array is rebuilt at that level and the previous dimension is is set to point to the new one. The garbage collector should clean up after us. Each scalar value in the example will have all tabs removed, but you can make the function do whatever you want. One more word of caution, this will take some major overhead if you want to iterate a deep data structure. I hope this does the trick.

¥peace out guys, thanks for reading my first post, CaMelRyder¥

In reply to Re: iterators: traversing arbitrary data structure by CaMelRyder
in thread iterators: traversing arbitrary data structure by dimar

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (13)
    As of 2014-10-31 15:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (219 votes), past polls