Skip to content

Instantly share code, notes, and snippets.

@symkat
Created December 29, 2011 03:29
Show Gist options
  • Save symkat/1531591 to your computer and use it in GitHub Desktop.
Save symkat/1531591 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use Time::HiRes qw/ time/;
sub unwrap_ref {
my ( @list ) = @_;
my @return;
for my $elem ( @list ) {
if ( ref $elem eq 'ARRAY' ) {
push @return, unwrap_ref( @{$elem} );
} else {
push @return, $elem;
}
}
return @return;
}
=head2 aref_to_list
Given an array of array refs of any depth, return a list
of non-ref scalars.
As a side effect, any other type in the array will be silently
disregarded.
=cut
sub aref_to_list {
my ( @list ) = @_;
my @return;
while ( my $elem = shift @list ) {
push @return, $elem unless ref $elem;
unshift @list, @{$elem} if ref $elem eq 'ARRAY';
}
return @return;
}
my $start = time;
print Dumper unwrap_ref( "foo", [ "bar", "blee", [ "baz" ], ], "taz" );
print "Above took " . ( time-$start ) . "\n";
$start = time;
print Dumper smush( "foo", [ "bar", "blee", [ "baz" ], ], "taz" );
print "Above took " . ( time-$start ) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment