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

Hi, Monks! My data structure is like:
my $kind = 'alph'; my $prop = 'single'; my $slice = { kind => $kind, prop => $prop, item => 5..11, };

but when I try to print the 'item', it always shows only one element inside the item.
my @item_2 = ($slice -> {item}); my $num = scalar @item_2; map {print "num is $num the $_\n";} @item_2;

How can I do to store an array to a pseudohash? Thanks in advance.

Replies are listed 'Best First'.
Re: how to store an array to a pseudohash?
by pc88mxer (Vicar) on Jun 28, 2008 at 01:43 UTC
    You need to store an array reference - it's like a pointer to the array:
    my $slice = { kind => $kind, prop => $prop, item => [ 5..11 ], };
    The square brackets create an array reference from the array 5..11 See perldoc perlref for more details.
Re: how to store an array to a pseudohash?
by toolic (Bishop) on Jun 28, 2008 at 01:45 UTC
    You can use square brackets to create an anonymous array. Then you can use @{ } to de-reference the array:
    use strict; use warnings; my $kind = 'alph'; my $prop = 'single'; my $slice = { kind => $kind, prop => $prop, item => [5..11] }; my @item_2 = @{$slice -> {item}}; my $num = scalar @item_2; map {print "num is $num the $_\n";} @item_2;

    prints:

    num is 7 the 5 num is 7 the 6 num is 7 the 7 num is 7 the 8 num is 7 the 9 num is 7 the 10 num is 7 the 11

    Is this what you are after?

Re: how to store an array to a pseudohash?
by TGI (Parson) on Jun 28, 2008 at 16:40 UTC

    As others have mentioned, psuedohashes were an experiment that didn't pan out, and are been deprecated in perl 5.8 and gone in perl 5.10.

    I thought I'd add to what others have shown you here by suggesting that you try out Data::Dumper. It is very helpful when trying to understand datastructures. Try this code:

    use Data::Dumper; my $slice = { kind => 'alph', prop => 'single', item => 5..11, } print Dumper $slice;

    While you are looking at perldoc.perl.org, check out perldsc--the data structures cookbook. There's good stuff in there on setting up nested structures.

    I also noticed that you use map to iterate over your @item_2 array, but don't use the return value--in other words, you are using map in a void context. Some people think this is a bad idea. Others don't have a problem with it. I don't really care, one way or the other. Anyhow, in case you decide you don't like void map, I'll show a couple of ways to do the same thing without raising that stylistic bugaboo:

    # print takes a list of arguments # here map generates a list of args for one call to print. print map { "num is $num the $_\n" } @item_2; # simply replaced map with foreach used as a statement modifier. print "num is $num the $_\n" foreach @item_2;


    TGI says moo

      Thanks for all the replies which are very instructive! (1) Thanks for leting me know that using following statement is referred to another hash which is not what I expected.
      my $slice = { kind => $kind, prop => $prop, item => 5..11 };

      (2) Thanks for pointing out the dereference method like:
      my @items = @{$slice -> {item}};
      I don't really understand why I need use @{} here to dereference, can somebody explain it for more details? I am just a Perl newbie :)
      (3) Thanks for present a good program for data structure viewer -- Data::Dumper. I guessed I heard of it sometime ago, now I know how to use it. It's a good toolkit for debugging too.
      (4) Thanks for criticizing my bad coding style (using map in a void context). I am digging into the link to find out why I should avoid to use this style... :)
        (4) Thanks for criticizing my bad coding style (using map in a void context). I am digging into the link to find out why I should avoid to use this style... :)

        Don't be too hard on yourself for this one. Void map/grep have been argued over for years in the perl community. Some don't see a problem with it, others do. Definitely look into the issue and decide for yourself what is right.

        I'm personally neutral on void map. I use foreach in most cases where a void map could be used, because I think it more clearly expresses the intent of the code. However, I would have no problem using a void map if I thought it made things more readable.

        You might like to check out this nice article on void map


        TGI says moo

        I don't really understand why I need use @{} here to dereference, can somebody explain it for more details?
        By using the square brackets, you have created a reference to an array. Since you want to access the elements of the array reference, you first need to de-reference the array. One way to do so is to use the @{} syntax. Here are some good resources on the topic:
Re: how to store an array to a pseudohash?
by philipbailey (Curate) on Jun 28, 2008 at 08:36 UTC
    Others have explained how to store an array in a hash. It might also be worth mentioning that your code is equivalent to:
    my $slice = { kind => $kind, prop => $prop, item => 5, 6, 7, 8, 9, 10, 11 };
    Which is also equivalent to:
    my $slice = { kind => $kind, prop => $prop, item => 5, 6 => 7, 8 => 9, 10 => 11 };
Re: how to store an array to a pseudohash?
by Narveson (Chaplain) on Jun 28, 2008 at 11:57 UTC

    I can't see where you are using the pseudohash feature. This is just as well, because the feature is deprecated.

    If you don't know what a pseudohash is, don't bother to find out. Just say hash.