Created
February 18, 2013 05:22
-
-
Save kentaro/4975262 to your computer and use it in GitHub Desktop.
flatten
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use YAML; | |
sub flatten { | |
my @list = @_; | |
map { | |
my $element = $_; | |
my @flattened; | |
if (ref $element eq 'ARRAY') { | |
push @flattened, flatten(@$_) | |
} | |
else { | |
push @flattened, $element | |
} | |
@flattened; | |
} @list | |
} | |
my @flattened = flatten([1, 2, 3], [4, 5, 6, [7, 8]], 9, 10); | |
print Dump(\@flattened); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment