Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: print_r

by mwah (Hermit)
on Sep 25, 2007 at 11:49 UTC ( [id://640906]=note: print w/replies, xml ) Need Help??


in reply to Re: print_r
in thread print_r

grinder:  This will recurse endlessly until the heat death of the universe or you run out of swap, whichever comes first. Then again, perhaps the PHP implementation does as well.

PHP's print_r (522) does indeed detect recursion after the first loop:
<?php $r1 = array( 1 ); $r2 = array( 2, &$r1 ); array_push( $r1, &$r2 ); print_r($r1); ?>

This will result in the following output:
( [0] => 1 [1] => Array ( [0] => 2 [1] => Array ( [0] => 1 [1] => Array *RECURSION* ) ) )

Regards

mwa

Replies are listed 'Best First'.
Re^3: print_r
by Anonymous Monk on Mar 23, 2009 at 16:30 UTC
    recursion check:
    sub print_r { my $level=""; my @level_index=(); my @print_r_Recursion=(); print_rec (@_); undef @level_index; undef @print_r_Recursion; undef $level; sub print_rec { if ( ! defined $level ) { $level = 0 }; if ( ! defined @level_index ) { $level_index[$level] = 0 }; for ( @_ ) { my $element = $_; my $index = $level_index[$level]; if (ref($element) eq 'ARRAY' || ref($element) eq 'HASH'){ my $loop=0; for (@print_r_Recursion){ if (($element."") eq ($_."")){ $loop=1; last; } } if ($loop==1){ print "\t" x $level . "[$index] =&gt; ". ref($elem +ent) ." *recursion*\n"; next; } push @print_r_Recursion,$element; } print "\t" x $level . "[$index] =&gt; "; if ( ref($element) eq 'ARRAY' ) { my $array = $_; $level_index[++$level] = 0; print "(Array)\n"; for ( @$array ) { print_rec( $_ ); } --$level if ( $level &gt; 0 ); } elsif ( ref($element) eq 'HASH' ) { my $hash = $_; print "(Hash)\n"; ++$level; for ( keys %$hash ) { $level_index[$level] = $_; print_rec( $$hash{$_} ); } } else { print "$element\n"; } $level_index[$level]++; } } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-24 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found