Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Array to hash refs

by Anonymous Monk
on Aug 13, 2002 at 15:50 UTC ( [id://189833]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have a problem that I thought I could solve by myself, but after thinking about it for far too long, I have realised that I am not experienced (or perhaps clever) enough. I want to turn an array of unknown length, for example @arr = ("a", "b", "c"), into hash refs: $hash->{a}->{b}->{c} = 1. If anyone knows how to do this I would be most grateful.

Replies are listed 'Best First'.
Re: Array to hash refs
by Abigail-II (Bishop) on Aug 13, 2002 at 16:01 UTC
    my @array = qw /a b c/; my $hash; my $val = 1; ($hash, $hash -> {pop @array}, $val) = (undef, $val, $hash) while @arr +ay > 1; $hash -> {pop @array} = $val;
    Abigail

      Even with jeffa's notes below, it still took me a while to wrap this around my brain but when I had, I came up with this....

      my @array = qw /a b c d e f/; my $hash; my $val = 1; ($hash->{pop @array}, $val, $hash) = ($val, $hash, undef) while @array +; $hash = $val;

      Which is slightly cleaner?, but that final assignment stood out so...

      my @array = qw /a b c d e f/; my $hash = 1; my $tmp; ($tmp->{pop @array}, $hash, $tmp) = ($hash, $tmp, undef) while @array +;

      Slightly non-intuative to assign the value to the hashref at the start but...

        return {map {$_ => 1} qw /a b c/}
        Or did I understand your questing wrong?

        Abigail

(jeffa) Re: Array to hash refs
by jeffa (Bishop) on Aug 13, 2002 at 16:06 UTC
    Here is all i could come up with:
    use strict; my @a = ('a'..'f'); my $hash; my $last = 1; for (reverse @a) { $hash->{$_} = $last; $last = {$_ => $hash->{$_}}; } delete $hash->{$a[$_]} for (1..@a);
    Hehehe, i waited until Abigail-II posted first though. Now i am going to spend some time figuring out how that works ... need more coffee!

    UPDATE: Ok, let's figure Abigail-II's code out ...

    When dealing with loops, a good way to understand the code is to chart out what happens at each iteration -
    • Iteration 1:
      • @array = ('a','b','c');
      • atomic:
        • $hash = undef;
        • $hash->{a} = $val; # $val = 1
        • $val = $hash;
      • # is ('b','c') > 1 ? yes
    • Iteration 2:
      • @array = ('b','c');
      • atomic:
        • $hash = undef;
        • $hash->{b} = $val; # $val = {a => 1}
        • $val = $hash;
      • # is ('c') > 1 ? no
    • Last line: (note that $hash is currently undef)
      • $hash->{c} = $val; # $val = {b => {a => 1}}
    The key to this code is the atomic triple assignment inside the while loop. Break that into 3 seperate assignments and the results are not the same. Abigail-II++ :)

    UPDATE*2:
    I think i like Anony's answer the best ... now that is using Perl to write Perl! /smack forehead

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      You can break it into 4 separate assignments though....

      Abigail

Re: Array to hash refs
by nat0m (Novice) on Aug 14, 2002 at 08:49 UTC
    my solution:
    use strict; my @array = qw/a b c d e f/; my $t = my $hashref = {}; while (@array) { $t = $t->{ shift @array } = @array>1 ? {} : 1 };
      Eval man!
      my @array = qw/a b c d/;
      
      my $ref = {};
      
      $code = '$ref->{'.join('}->{', @array).'} = 1;';
      
      eval $code;
      
      use Data::Dumper;
      print Dumper($ref);
      
Re: Array to hash refs
by Anonymous Monk on Aug 14, 2002 at 07:14 UTC
    Thanks for all your help!
Re: Array to hash refs
by jackdied (Monk) on Aug 14, 2002 at 16:31 UTC
    It sounds like you might be trying to write Tree::Trie, which creates a nested, searchable list based on the characters in it.

    Check it out, and also the thread word association problem

    -jackdied

Re: Array to hash refs
by deMize (Monk) on Oct 11, 2012 at 14:47 UTC
    I got here from Google and this is old but: $h={@arr};
    And if you prefer 1 to undef then:
    push @arr,1 if @arr % 2; $h={@arr};
    Nevermind: I misread the question, which was asking for a tree -- moderator, please delete this answer.


    Demize

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://189833]
Approved by thelenm
Front-paged by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found