convert string of keys to multidimensional hash

Discussion in 'Perl Misc' started by dt, Feb 16, 2007.

  1. dt

    dt Guest

    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
    dt, Feb 16, 2007
    #1
    1. Advertising

  2. dt

    Guest

    "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


    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
    , Feb 17, 2007
    #2
    1. Advertising

  3. 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::Dumper;

    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
    Mark Clements, Feb 17, 2007
    #3
  4. On 17 Feb, 08:08, Mark Clements <>
    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;
    Brian McCauley, Feb 17, 2007
    #4
  5. dt

    Mirco Wahab Guest

    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
    Mirco Wahab, Feb 18, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    499
    red floyd
    Nov 10, 2011
  2. Xeno Campanoli
    Replies:
    16
    Views:
    257
    Martin DeMello
    Aug 25, 2005
  3. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    132
    George Ogata
    Apr 15, 2006
  4. Michael ..
    Replies:
    12
    Views:
    201
    Julian Leviston
    Feb 16, 2009
  5. dt

    convert string to hash of hash?

    dt, Mar 1, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    456
    ~greg
    Mar 2, 2007
Loading...

Share This Page