Removing nested hash deserialization from hash key lists

I have a string that I would like to "unflatten" or "tree-ify"; that is, I want to go from this:

F=8|A_C=3|A_B=2|D_G_H=11|D_B=2|E=5  

:

{
  A => {
    B => 2,
    C => 3,
  },
  D => {
     B => 2,
     G => {
       H => 11,
     },
  },
  E => 5,
  F => 8,
}

My strategy was to process each field with delimiters separately and split into characters =into a key / value pair:

sub unflatten {
    my ($data) = @_;
    my @fields = split /\|/, $data;
    my $result = {};

    for my $datum (@fields) {
            my ($key, $value) = split /=/, $datum;
            $result->{&processline($key)} = $value;
    }
    return $result;
}

I am trying recursive magic in processline:

sub processline {
    my ($key) = @_;

    my ($first, $rest) = split /_/, $key, 2; # split key into at most 2 parts
    if($rest) {
            return { $first => &processline($rest) }; 
            # if the key is nested, there will be something in $rest
            # so recursively process the smaller $rest, and build up the result hashref
    }
    else {
            return $first;
    }
}

Unfortunately this does not work:

my $header = "F=8|A_C=3|A_B=2|D_G_H=11|D_B=2|E=5";
use Data::Dumper;
print Dumper &unflatten($header);

When I do this, I get:

$VAR1 = {
      'F' => '8',
      'HASH(0xe9af60)' => '2',
      'HASH(0xe9ae28)' => '11',
      'E' => '5',
      'HASH(0xe9af90)' => '3',
      'HASH(0xe9ae40)' => '2'
    };

Can someone explain the thought process for a recursive solution, or suggest where my Perl went so badly? Disappointingly, I could easily handle this function (smooth out).

+4
source share
1 answer

, for, . , , processline , .

, -,

unflatten($hash, 'key1_key2_key3_key4', 'value')

unflatten($hash->{key1}, 'key2_key3_key4', 'value')`

. $hash, .

sub unflatten {

  my $result = {};

  for my $item (split /\|/, $_[0]) {

    my ($keys, $value) = split /=/, $item;
    my @keys = split /_/, $keys;
    my $hash = $result;

    while (@keys > 1) {
      my $key = shift @keys;
      $hash->{$key} ||= {};
      $hash = $hash->{$key};
    }

    $hash->{$keys[0]} = $value;
  }

  return $result;
}

$VAR1 = {
      'A' => {
               'C' => '3',
               'B' => '2'
             },
      'F' => '8',
      'D' => {
               'G' => {
                        'H' => '11'
                      },
               'B' => '2'
             },
      'E' => '5'
    };

, , .

use strict;
use warnings;

use Data::Dumper;

my $data = 'F=8|A_C=3|A_B=2|D_G_H=11|D_B=2|E=5';

my $result = {};
unflatten2($result, $_) for split /\|/, $data;
print Dumper $result;

sub unflatten2 {
  my ($hash, $data) = @_;

  if ($data =~ /_/) {
    my ($key, $rest) = split /_/, $data;
    unflatten2($hash->{$key} ||= {}, $rest);
  }
  else {
    my ($key, $val) = split /=/, $data;
    $hash->{key} = $val;
  }
}

Data::Diver , ,

, , ,

use strict;
use warnings;

use Data::Diver qw/ DiveVal /;
use Data::Dumper;

my $data = 'F=8|A_C=3|A_B=2|D_G_H=11|D_B=2|E=5';

my $result = {};    

for (split /\|/, $data) {
  my ($keys, $val) = split /=/;
  DiveVal($result, split /_/, $keys) = $val;
}

print Dumper $result;
+6

Source: https://habr.com/ru/post/1543645/


All Articles