]> git.vpit.fr Git - perl/modules/autovivification.git/blobdiff - samples/hash2array.pl
Handle array and hash slices
[perl/modules/autovivification.git] / samples / hash2array.pl
index 8b3419d1139fbdbb68f742470b85125bbc8aec40..e70b69be8ea90e83c558a527dcc862c8a0beba67 100644 (file)
@@ -10,7 +10,10 @@ open my $hash_t,       '<', 't/20-hash.t';
 open my $array_t,      '>', 't/30-array.t';
 open my $array_fast_t, '>', 't/31-array-fast.t';
 
-sub num { ord($_[0]) - ord('a') }
+sub num {
+ my ($char) = $_[0] =~ /['"]?([a-z])['"]?/;
+ return ord($char) - ord('a')
+}
 
 sub hash2array {
  my ($h) = @_;
@@ -46,7 +49,12 @@ while (<$hash_t>) {
   for my $file ([ 1, $array_t ], [ 0, $array_fast_t ]) {
    local $_ = $_;
    s!(\ba\b)?(\s*)HASH\b!($1 ? 'an': '') . "$2ARRAY"!eg;
-   s!->{([a-z])}!my $n = num($1); '->[' . ($file->[0] ? "\$N[$n]" : $n) .']'!eg;
+   s{
+    {\s*(['"]?[a-z]['"]?(?:\s*,\s*['"]?[a-z]['"]?)*)\s*}
+   }{
+    '[' . join(', ', map { my $n = num($_); $file->[0] ? "\$N[$n]" : $n }
+                      split /\s*,\s*/, $1) . ']'
+   }gex;
    s!%(\{?)\$!\@$1\$!g;
    my $buf;
    my $suffix = $_;