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

Re^3: Recursive function always returns 0

by liverpole (Monsignor)
on Feb 14, 2007 at 21:27 UTC ( [id://600085]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Recursive function always returns 0
in thread Recursive function always returns 0

Good job!

I'm still going to recommend that you stop using prototypes.  Not using prototypes is a lesson I learned from my wise fellow monks.  This article is a "must read" on the subject.

You can also simplify your code somewhat.  The following are just suggestions, of course (ultimately you should do what makes the most sense for you), but here are some things to consider ...

Why not pull all your arguments out of @_ in one fell swoop, with my ($hash, @row) = @_?

When I see an if { ... } else { ... } where the first block is fairly long and the second very short, I often like to reverse them, so the short block is first.  Then you can often get rid of the else clause as well.  So instead of:

if(ref($hash) eq 'HASH'){ my $header = $hash->{'header'}; while(my ($key, $value) = each(%$hash)){ push(@stack, unpack_row($value, (@row, $key))); } } else { return([@row, $hash]); }

I prefer the more "balanced" looking:

if(ref($hash) ne 'HASH'){ return([@row, $hash]); } my $header = $hash->{'header'}; while(my ($key, $value) = each(%$hash)){ push(@stack, unpack_row($value, (@row, $key))); }

Then I noticed you're not using $header, so you can remove that line.

Additionally, you can make the construction of @stack within the loop into a map statement, and then throw away @stack entirely, and just return the result from map.

Here's the entire program (with a few other simplifications):

use strict; use warnings; use Data::Dumper; sub unpack_row { my ($hash, @row) = @_; return [ @row, $hash ] unless (ref $hash eq 'HASH'); return map { unpack_row($hash->{$_}, @row, $_) } keys %$hash; } my $hash = { foo => { bar => { bat => 5, bird => 2 } }, goo => { tab => { bird => 2 } }, }; print Dumper($hash); print Dumper(unpack_row($hash, ()));

Isn't that a bit easier to follow?


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2025-12-07 04:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (85 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.