Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-19 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found