[ create a new paste ] login | about

Link: http://codepad.org/xVPuCO1N    [ raw code | output | fork ]

vol7ron - Perl, pasted on Oct 13:
use Data::Dumper;

   my $foo = "a:b:c:d:a";
   my $bar = "a:b:c:d:z";
   my $hoh = {};

   sub createHash {
      my ($hoh,$str_orig,$str_rest,$lastkey,$parent) = @_;

      $str_rest = $str_rest || $str_orig || "";
      $_        = $str_rest;

      if (/^(.*?):(.*)$/)
      { 
         $parent    = $hoh;
         $hoh->{$1} = $hoh->{$1} || {};
         createHash($hoh->{$1},$str_orig,$2,$1,$parent);
      } 
      elsif (defined($lastkey)) 
      {
         delete($parent->{$lastkey}) if ref $parent->{$lastkey} ne "ARRAY";
         push (@{$parent->{$lastkey}} , [$str_rest,$str_orig]);
      }
      return $hoh;
   }
   $hoh = createHash($hoh,$foo);
   $hoh = createHash($hoh,$bar);

   print Dumper($hoh);


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$VAR1 = {
          'a' => {
                   'b' => {
                            'c' => {
                                     'd' => [
                                              [
                                                'a',
                                                'a:b:c:d:a'
                                              ],
                                              [
                                                'z',
                                                'a:b:c:d:z'
                                              ]
                                            ]
                                   }
                          }
                 }
        };


Create a new paste based on this one


Comments: