Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Parse data representing hash

by hdb (Monsignor)
on Jun 29, 2014 at 17:23 UTC ( #1091649=note: print w/replies, xml ) Need Help??

in reply to Parse data representing hash

Here is my humble attempt to solve your interesting puzzle:

use strict; use warnings; use Data::Dumper; my %hash; my @row; while(<DATA>){ my ($level, $word) = /^(\s*)(\w*)$/; $level = length($level)/4; $row[$level] = $word; $#row = $level; my $last = \%hash; for (0..$#row) { $last->{$row[$_]} = $_ == $#row ? undef : {} if not defined $last- +>{$row[$_]}; $last = $last->{$row[$_]}; } } print Dumper \%hash; __DATA__ one two three four five six

Replies are listed 'Best First'.
Re^2: Parse data representing hash
by peterp (Sexton) on Jun 29, 2014 at 18:47 UTC

    Thank you very much for providing your attempt, its very similar to my alternative version to using Data::Diver, but has provided some insight into ways I can improve. Notably, the $#array = $index syntax is new to me and is a nice alternative to using splice or a slice. Also, I prefer your for loop over my recursive function to construct the resultant hash.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1091649]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2018-01-23 14:03 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (246 votes). Check out past polls.