Hi jpfarmer,

imp's answer is correct.  The whole point of recursion is, of course, that you have to propogate the answer back up the stack.  In your case, you were discarding the return value from unpack_row, so the calling subroutine (also unpack_row) didn't get anything to work with.

Here's a more-or-less working version of your code.  Note the return statement in the if(ref($hash) eq 'HASH') block.  You may have to modify what you're returning; as I'm not exactly sure what you're trying to do.

use strict; use warnings; use Data::Dumper; # Prototype defined to avoid 'function not defined' warnings # on compilation. sub unpack_row{ my $hash = shift; my @row = @_; if(ref($hash) eq 'HASH'){ my $header = $hash->{'header'}; my @results = ( ); while(my ($key, $value) = each(%$hash)){ # Save the results the way you need them... push @results, unpack_row($value, (@row, $key)); } return (@results); # ... or whatever you return } else { print Dumper((@row, $hash)); return (@row, $hash); } } my $hash = {}; $hash->{'foo'}->{'bar'}->{'bat'} = 5; print unpack_row($hash, ());

I'll point out that you don't need prototypes; just leave off the parentheses "( ... )" when you define the subroutine unpack_row and you'll be all set.

Also, you don't need to shift @_; @_ is the thing shifted by default.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: Recursive function always returns 0 by liverpole
in thread Recursive function always returns 0 by jpfarmer

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.