Skip to content

Instantly share code, notes, and snippets.

@kentaro
Created February 18, 2013 05:22
Show Gist options
  • Save kentaro/4975262 to your computer and use it in GitHub Desktop.
Save kentaro/4975262 to your computer and use it in GitHub Desktop.
flatten
#!/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