Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

convert string of keys to multidimensional hash

 
 
dt
Guest
Posts: n/a
 
      02-16-2007
how do I do this:

convert a string such as "dim1:dim2:dim3"
or array ("dim1", "dim2", "dim3")

to a hash like:
$hash{"dim1"}{"dim2"}{"dim3"}

any help is appreciated

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-17-2007
"dt" <(E-Mail Removed)> wrote:
> how do I do this:
>
> convert a string such as "dim1:dim2:dim3"
> or array ("dim1", "dim2", "dim3")
>
> to a hash like:
> $hash{"dim1"}{"dim2"}{"dim3"}
>
> any help is appreciated


You want to convert data into source-code? Are you sure this is what
you really want to do?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      02-17-2007
dt wrote:
> how do I do this:
>
> convert a string such as "dim1:dim2:dim3"
> or array ("dim1", "dim2", "dim3")
>
> to a hash like:
> $hash{"dim1"}{"dim2"}{"dim3"}
>


I'm not entirely sure what you're after, but:

F:\Documents and Settings\Mark3>cat hash.pl
use strict;
use warnings;

use Data:umper;

my $string = "dim1:dim2:dim3";

my @items = split /:/,$string;

my %hash = ();

my $lastItem = \%hash;

foreach my $item(@items){
my $newItem = {};
$lastItem->{$item} = $newItem;
$lastItem = $newItem;


}

print Dumper \%hash;


__END__

F:\Documents and Settings\Mark3>perl hash.pl
$VAR1 = {
'dim1' => {
'dim2' => {
'dim3' => {}
}
}
};

may get you started.

Mark
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      02-17-2007
On 17 Feb, 08:08, Mark Clements <(E-Mail Removed)>
wrote:
> dt wrote:
> > how do I do this:

>
> > convert a string such as "dim1:dim2:dim3"
> > or array ("dim1", "dim2", "dim3")

>
> > to a hash like:
> > $hash{"dim1"}{"dim2"}{"dim3"}


> my %hash = ();
>
> my $lastItem = \%hash;
>
> foreach my $item(@items){
> my $newItem = {};
> $lastItem->{$item} = $newItem;
> $lastItem = $newItem;
>
> }


This is a very common problem and there's a very simple way to do it
avoiding the last empty hash:

my $lastItem = \\%hash;
$lastItem = \$$lastItem->{$_} for @items;

 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      02-18-2007
dt wrote:
> how do I do this:
>
> convert a string such as "dim1:dim2:dim3"
> or array ("dim1", "dim2", "dim3")
>
> to a hash like:
> $hash{"dim1"}{"dim2"}{"dim3"}
>
> any help is appreciated


after reading Brians excellent reply
(which was somehow new for me), I'd
like to add 2 "functional forms" to
the discussion (recursive), one
ends up with 'undef' on the chain,
the other with a clean hash ref:

...

# end up w/hash ref:
sub rh2arr { rh2arr($_[0]->{$_[1]}={}, @_[2..@_-1]) if @_>1 }

#end up w/undef
sub rrh2arr { rrh2arr(\${$_[0]}->{$_[1]}, @_[2..@_-1]) if @_>1 }

# usage
my @arr = qw'dim1 dim2 dim3';
my (%hash1, %hash2);

rh2arr \%hash1, @arr;
print Dumper \%hash1;

rrh2arr \\%hash2, @arr; # learned \\this from Brian's post
print Dumper \%hash2;

...


Regards (and thanks to Brian)

Mirco
 
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
Multidimensional hash - get all not blank keys Michael .. Ruby 12 02-16-2009 02:02 AM
convert string to hash of hash? dt Perl Misc 1 03-02-2007 04:57 AM
Hash#values and Hash#keys order Alex Fenton Ruby 1 04-15-2006 05:45 AM
No Keys, nor other hash methods on multidimensional hash Xeno Campanoli Ruby 16 08-25-2005 07:04 AM



Advertisments