Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > convert string to hash of hash?

Reply
Thread Tools

convert string to hash of hash?

 
 
dt
Guest
Posts: n/a
 
      03-01-2007
I have a data set such as this:

$ref{"a"}{"b"}[0]{"c"}

any way to take a string such as:

"a:b:[0]:c" and get the value stored in $ref{"a"}{"b"}[0]{"c"}?

does using Data:umper help?

I want to be able to put a string in a template file which gets the
value from the datastructure,

 
Reply With Quote
 
 
 
 
~greg
Guest
Posts: n/a
 
      03-02-2007

"dt" > wrote...
>I have a data set such as this:
>
> $ref{"a"}{"b"}[0]{"c"}
>
> any way to take a string such as:
>
> "a:b:[0]:c" and get the value stored in $ref{"a"}{"b"}[0]{"c"}?
>
> does using Data:umper help?


Yes! Always does!
~~

When I was trying understand perl references I came up with
the following subroutine: "Node()".

It is essentially the same idea that Jim Gibson mentioned,
but much more general.

The syntax for its use is:

${ Node( \$structure, $index1, $index2,...) }

This can be both assigned to, and read from, just as is!

The indices are either text or integers.
(--taken to be integral iff =~ /^\d+$/)

The first argument to Node() is a pointer to a pointer to a structure.
The $structure can be initially undefined.

I don't see that any special error-checking is required.
However there is one cavat.

If you write to (or even try to read from)
incompatible signatures, then you loose information.

"signature" meaning the specific sequence of the argument types,
- integers (for arrays) and text ( for hashes).

If, in the same argument place, you once use a text
index, but then later use an integer, it's not an error.
But the whole hash branch at that point gets lost.

An example of this is included below (--the "busted").

~greg

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

use strict;
use warnings;
use Data:ump qw(dump);
$|=1;

sub Node
{
my $p = shift;
foreach my $i (@_)
{
if($i =~ /^\d+$/)
{
$$p=[] if ref($$p) ne 'ARRAY';
$$p->[$i] = undef if ! exists $$p->[$i];
$p = \$$p->[$i];
}
else
{
$$p={} if ref($$p) ne 'HASH';
$$p->{$i}=undef if ! exists $$p->{$i};
$p = \$$p->{$i};
}
}
return $p;
}


my $Struct;

${Node( \$Struct, (split /\W+/, "a:b:[0]:c") ) } = 'this is ';
${Node(\$Struct, 'a','b',3,'d','e') } = 'a test';

print ${Node(\$Struct,'a','b',0,'c')};
print ${Node(\$Struct,'a','b',3,'d','e')};

print "\n";
dump($Struct);


print "~~~~~~~~~~~~~~~~~~~~~\n";
# An example of loosing information
# by assigning to a different signature:
# (--the 'b' and the '3' have been interchanged)

${Node(\$Struct, 'a',3,'b','d','e') } = 'busted!';

#print ${Node(\$Struct,'a','b',0,'c')}; # error (undef)
#print ${Node(\$Struct,'a','b',3,'d','e')}; # error (undef)

print ${Node(\$Struct, 'a',3,'b','d','e') };
print "\n";
dump($Struct);

__END__

PRINTS:...

this is a test
{
a => {
b => [{ c => "this is " }, undef, undef, { d => { e => "a test" } }],
},
}
~~~~~~~~~~~~~~~~~~~~~
busted!
{
a => [undef, undef, undef, { b => { d => { e => "busted!" } } }],
}




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
How do I convert string/text from column to hash ??? Fransiscus Xaverius Ruby 0 12-13-2007 04:41 PM
make a hash object to string object as look like a hash ? Pokkai Dokkai Ruby 8 11-18-2007 05:23 AM
Convert String to Hash Nick Snels Ruby 7 03-09-2006 12:41 PM



Advertisments